我正在使用Xcode 4.3.2。以编程方式加载笔尖
我想以编程方式加载一个Nib而不是使用Storyboards。故事板看起来简单很多,但不幸的是,它们与iOS 4不兼容。我正在构建一个需要向后兼容的应用程序。
这是我的问题:我试图加载主视图 - 一个绿色背景的简单视图。目前没有默认视图。
我的文件有:
AppDelegate.h/.M GreenViewController.h/.M SwitchViewController.h/.M
我想绿屏时,应用程序开始所以在加载AppDelegate.m我:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.switchViewController = [[SwitchViewController alloc] initWithNibName:@"GreenView" bundle:nil];
//UIView *switchView = self.switchViewController.view;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
在SwitchViewController.m我:
- (void)viewDidLoad
{
self.greenViewController = [[GreenViewController alloc] initWithNibName:@"GreenView" bundle:nil];
[self.view insertSubview:self.greenViewController.view atIndex:0];
[super viewDidLoad];
}
用简单的initWithNibName填入默认代码。
在GreenViewController.m我:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
这里是一切的主要功能。当我运行应用程序,我立刻得到错误:
2012-10-09 12:34:35.586 MyViewSwitcher[5210:f803] -[AppDelegate setSwitchViewController:]: unrecognized selector sent to instance 0x6c6c310
2012-10-09 12:34:35.587 MyViewSwitcher[5210:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate setSwitchViewController:]: unrecognized selector sent to instance 0x6c6c310'
是否有人可以帮助我理解我的错误的原因是什么?我确定了GreenView.xib的“Custom Class”是Identity Inspector中的“GreenViewController”。另外,我确定“查看”的引用出口是文件的所有者。我想在我的应用程序启动时加载GreenView笔尖。最后,我会通过添加一个按钮来切换视图,但到目前为止,我还没有能够加载主视图。我不确定我的误解。
非常感谢您的帮助!
你有正确合成你的'switchViewController'财产? –
Adil,那绝对是我的问题。我添加了“@synthesize switchViewController;” &现在我的错误是:2012-10-09 13:48:27.970 MyViewSwitcher [5345:f803]应用程序窗口预计在应用程序启动结束时有一个根视图控制器 – Henry
您可以给我这个手吗?我想我正走上正轨。 – Henry