使用NavigationController按下Button时,iOS应用程序崩溃

时间:2012-03-02 12:36:20

标签: ios button exc-bad-access sigabrt navigationcontroller

我是objective-c的新手,我目前正在开发iOS应用程序。 我有一个Button我调用SettingsButton,它位于自定义UIView对象中。 当我按下这个按钮时,FileOwner的触摸内部处理程序" ViewController.m"被调用,稍后将推送一个ViewController在NavigationController上。 但应用程序与SIGABRT或EXC_BAD_ACCESS崩溃。 这是因为我在AppDelegate.m中使用NavigationController插入代码 有什么想法吗?

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *vc;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    vc = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
    vc = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}

UINavigationController* navController =
[[UINavigationController alloc] initWithRootViewController:vc];
navController.navigationBarHidden = true;
[self.window addSubview:navController.view];

[self.window makeKeyAndVisible];
return YES;
}

ViewController.m:

- (IBAction)SettingsPressed:(id)sender {
NSLog(@"SettingsPressed!");
}

要明确:我的目的只是在按下SettingsButton并使用Back-Button返回时更改视图。但该应用程序已经与空的Eventhandler崩溃。

发生sigabrt时

错误消息

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString SettingsPressed:]: unrecognized selector sent to instance 0x7a06fc0'

2 个答案:

答案 0 :(得分:1)

我刚刚解决了。 我必须在AppDelegate.h中插入一个属性并在AppDelegate.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.
UIViewController *vc;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    vc = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
    vc = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}

_navController = [[UINavigationController alloc] initWithRootViewController:vc];
_navController.navigationBarHidden = true;
[self.window addSubview:_navController.view];
[self.window makeKeyAndVisible];
return YES;
}

这么大的错误带来了如此大的影响。 但是我真的不知道上面的代码导致错误的原因。也许任何人都可以澄清?

答案 1 :(得分:0)

当然,您正在将消息发送到错误的对象,在代码中运行查找以查看是否将消息SettingsPressed直接发送到NSString并检查您是否在界面生成器中建立了正确的连接。您也可以发送调试器po addressOfCrashingInstance以接收更多信息。