我正在创建基于导航的基本应用程序,并且看到了exc_bad_access错误。有人可以指点我有什么不对吗?我只有2个屏幕,这是我正在使用的代码:
AppDelegate.m中的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *navcon = [[UINavigationController alloc]init];
psLaunchVC* pvc = [[psLaunchVC alloc]init];
[navcon pushViewController:pvc animated:NO];
[self.window addSubview:navcon.view];
[self.window makeKeyAndVisible];
return YES;
}
psLaunchVC作为第一个屏幕出现。 我正在尝试从psLaunchVC启动psTipVC。
psLaunchVC有一个在.h中声明为
的动作-(IBAction)showTip:(id)sender;
并在.m中实现
- (IBAction) showTip:(id)sender
{
// psTipVC *pst = [[psTipVC alloc]init];
psTipVC *pst = [[psTipVC alloc]initWithNibName:@"psTipVC" bundle:nil];
[self.navigationController pushViewController:pst animated:YES];
}
showTip在IB中连接为touchUpInside的操作。但是,执行此代码时,我看到一个exc_bad_access错误。有人可以帮我解决这里的问题吗? self.navigationController是访问导航控制器的正确方法吗?
供参考声明:
@interface psLaunchVC : UIViewController
@interface psTipVC : UIViewController
实际错误消息:
2011-12-29 00:03:13.739 passport[633:707] -[__NSCFString showTip:]:
unrecognized selector sent to instance 0x18f5e0
2011-12-29 00:03:13.748 passport[633:707]
*** Terminating app due to uncaught exception
'NSInvalidArgumentException',
reason: '-[__NSCFString showTip:]:
unrecognized selector sent to instance 0x18f5e0'
答案 0 :(得分:1)
您获得的消息看起来像psLaunchVC已经被释放,并且插座指向垃圾。你是否可以在启用ARC的情况下编译它?如果是这样,当应用程序:didFinishLaunchingWithOptions:你的导航控制器最有可能从你下面释放出来,因为它没有被任何人保留。尝试将该方法重新设计为如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
psLaunchVC* pvc = [[psLaunchVC alloc]init];
UINavigationController *navcon = [[UINavigationController alloc]initWithRootViewController:pvc];
self.window.rootViewController = navcon;
[self.window makeKeyAndVisible];
return YES;
}
UIWindow真的想要一个rootViewController,而不仅仅是要显示的视图。另外,initWithNibName:bundle:是UIViewControllers的指定初始化程序,psLaunchVC应该用它初始化。