我使用的是Xcode 4.3.2。
我正在尝试以编程方式加载一个Nib,而不是使用故事板。故事板看起来简单得多,但不幸的是它们与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的“自定义类”是身份检查器中的"GreenViewController“。此外,我还确保"View“的引用出口是该文件的所有者。我想在我的应用程序启动时加载GreenView笔尖。最后,我将通过添加一个按钮来切换视图,但到目前为止,我甚至还不能加载主视图。我不确定我误解了什么。
提前感谢您的帮助!
发布于 2012-10-09 21:40:05
问题不在于装入笔尖。问题就在下面这一行:
self.switchViewController = [[SwitchViewController alloc] initWithNibName:@"GreenView" bundle:nil];
这里使用的是“点语法”,中对此进行了解释。当您使用该语法设置属性时,编译器会将其转换为常规的Objective-C消息,如下所示:
[self setSwitchViewController:[[SwitchViewController alloc] initWithNibName:@"GreenView" bundle:nil]];
在运行时,系统会告诉您AppDelegate
对象不理解setSwitchViewController:
方法。您需要使用中解释的@synthesize
指令来告诉编译器为您实现setSwitchViewController:
方法:
@synthesize switchViewController = _switchViewController;
发布于 2012-10-09 19:47:33
'-[AppDelegate setSwitchViewController:]: unrecognized selector sent to instance 0x6c6c310'
您正在向/在AppDelegate中发送消息(调用该方法) setSwitchViewController:
。这就是问题所在。
发布于 2012-10-10 01:25:07
UIWindow
具有rootViewController
,然后再使用makeKeyAndVisible
makeKeyAndVisible;
SwitchViewController.m
,什么时候调用super
方法是很重要的。除非您有任何特殊意图,否则几乎总是在调用本地self
实现之前调用super
实现。因此,我建议您将SwitchViewController.m
中的- (void)viewDidLoad
更改为类似于- (void)viewDidLoad { [super viewDidLoad];
self.greenViewController = [GreenViewController alloc:@“GreenView”包:nil];
self.view insertSubview:self.greenViewController.view atIndex:0;}
https://stackoverflow.com/questions/12807322
复制相似问题