1

我正在阅读 Apress 的 iOS5 开发初期的多视图应用程序章节,并遇到了从其中一个视图上的按钮连接到文件所有者的问题。程序的一般前提是使用一个根控制器 BIDSwitchViewController 在 BIDBlueViewController 和 BIDYellowViewController 两个内容视图之间切换。每个内容视图都有一个按钮,将 Touch Up Inside 事件连接到文件的所有者。我在 BIDBlueViewController 中建立连接没有问题,但是当我尝试在 BIDYellowViewController 中建立连接时,我无法在 Touch Up Inside 事件和文件所有者图标之间建立连接。

书中的说明

将一个圆形矩形按钮从库中拖到视图中,使用指南将按钮在视图中垂直和水平居中。双击该按钮,并将其标题更改为 Press Me。接下来,在按钮仍处于选中状态的情况下,切换到连接检查器,从 Touch Up Inside 事件拖动到文件所有者图标,然后连接到 blueButtonPressed 操作方法

我可以毫无问题地做到这一点,但是当我尝试为黄色视图完成此说明时

将一个圆形矩形按钮从库中拖到视图中,使用指南将按钮在视图中垂直和水平居中。双击该按钮,并将其标题更改为 Press Me。接下来,在按钮仍处于选中状态的情况下,切换到连接检查器,从 Touch Up Inside 事件拖动到 File's Owner 图标,然后连接到 yellowButtonPressed 操作方法

文件所有者图标不允许我建立连接,因为我从 Touch Up Inside 事件拖动到文件所有者图标文件所有者从不突出显示并且我无法建立连接。

这是我的代码

根控制器接口

//
//  BIDSwitchViewController.h
//  ViewSwitcher
//
//  

#import <UIKit/UIKit.h>

@class BIDYellowViewController;
@class BIDBlueViewController;

@interface BIDSwitchViewController : UIViewController

@property (strong, nonatomic) BIDYellowViewController *yellowViewController;
@property (strong, nonatomic) BIDBlueViewController *blueViewController;

- (IBAction)switchViews:(id)sender;

@end

根控制器实现

//
//  BIDSwitchViewController.m
//  ViewSwitcher
//

#import "BIDSwitchViewController.h"
#import "BIDYellowViewController.h"
#import "BIDBlueViewController.h"

@implementation BIDSwitchViewController
@synthesize yellowViewController;
@synthesize blueViewController;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.blueViewController = [[BIDBlueViewController alloc]
                               initWithNibName:@"BlueView" bundle:nil];
    [self.view insertSubview:self.blueViewController.view atIndex:0];
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)switchViews:(id)sender{
    if (self.yellowViewController.view.superview == nil){
        if (self.yellowViewController == nil) {
        self.yellowViewController = 
    [[BIDYellowViewController alloc] initWithNibName: @"YellowView" 
                                              bundle:nil];
    }
    [blueViewController.view removeFromSuperview];
    [self.view insertSubview:self.yellowViewController.view atIndex:0];
} else {
    if (self.blueViewController == nil){
        self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:@"BlueView" 
                                                                          bundle:nil];
    }
    [yellowViewController.view removeFromSuperview];
    [self.view insertSubview:self.blueViewController.view atIndex: 0];
    }
}



- (void) didReceiveMemoryWarning {
    // Release the view if it doesn't have a super view
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc, that aren't in use
    if (self.blueViewController.view.superview == nil){
        self.blueViewController = nil;
    } else {
        self.yellowViewController = nil;
    }
}

@end

蓝色内容视图控制器界面

//
//  BIDSwitchViewController.h
//  ViewSwitcher
//

#import <UIKit/UIKit.h>

@class BIDYellowViewController;
@class BIDBlueViewController;

@interface BIDSwitchViewController : UIViewController

@property (strong, nonatomic) BIDYellowViewController *yellowViewController;
@property (strong, nonatomic) BIDBlueViewController *blueViewController;

- (IBAction)switchViews:(id)sender;

@end

蓝色内容视图控制器实现

//
//  BIDBlueViewController.m
//  ViewSwitcher
//
//  

#import "BIDBlueViewController.h"

@interface BIDBlueViewController ()

@end

@implementation BIDBlueViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

黄色内容视图控制器界面

//
//  BIDYellowViewController.h
//  ViewSwitcher
//

#import <UIKit/UIKit.h>

@interface BIDYellowViewController : UIViewController

- (IBAction)yellowButtonPressed;

@end

黄色内容视图控制器实现

//
//  BIDYellowViewController.m
//  ViewSwitcher
//
//  

#import "BIDYellowViewController.h"

@interface BIDYellowViewController ()

@end

@implementation BIDYellowViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
4

0 回答 0