我正在研究Apress的iOS5开始的多视图应用程序章节,并遇到了从其中一个视图上的按钮到文件所有者的连接问题。该程序的一般前提是使用根控制器,BIDSwitchViewController在两个内容视图之间切换BIDBlueViewController和BIDYellowViewController。每个内容视图都有一个按钮,将Touch Up Inside事件连接到文件所有者。我在BIDBlueViewController中建立连接没有问题,但是当我尝试在BIDYellowViewController中建立此连接时,我无法在Touch Up Inside事件和文件所有者图标之间建立连接。
书中的说明
使用。将圆形矩形按钮从库中拖到视图上 在视图中居中按钮的指南,包括垂直和 水平。双击该按钮,将其标题更改为按 我。接下来,仍然选中该按钮,切换到连接 检查员从Touch Up Inside事件拖动到 文件的所有者图标,并连接到blueButtonPressed操作方法
我可以毫无问题地执行此操作,但是当我尝试完成黄色视图的此说明时
使用。将圆形矩形按钮从库中拖到视图上 在视图中居中按钮的指南,包括垂直和 水平。双击该按钮,将其标题更改为按 我。接下来,仍然选中该按钮,切换到连接 检查员从Touch Up Inside事件拖动到 文件的所有者图标,并连接到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