按下带导航控制器的按钮时iOS应用程序崩溃
问题描述:
我是新来的objective-c,我目前正在开发iOS应用程序。 我有一个按钮,我打电话给SettingsButton,这是一个自定义的UIView对象。 当我按下此按钮时,调用FileOwner“ViewController.m”的Touch Up Inside处理程序,稍后将在NavigationController上推送ViewController。 但是,该应用程序与SIGABRT或EXC_BAD_ACCESS崩溃。 这是因为我在AppDelegate.m中插入了带有NavigationController的代码 有没有想法?按下带导航控制器的按钮时iOS应用程序崩溃
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并具有返回 - 当按钮返回。但该应用程序已经崩溃与空事件处理程序。当SIGABRT发生
ERRORMESSAGE:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString SettingsPressed:]: unrecognized selector sent to instance 0x7a06fc0'
答
当然你将消息发送到了错误的对象,在你的代码运行查找,看是否要发送直接SettingsPressed到NSString的消息,并检查你是否在界面构建器中建立了正确的连接。您也可以发送调试程序po addressOfCrashingInstance
以接收更多信息。
答
我刚刚解决了。 我在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;
}
这样的小错误。 但是不知道为什么我的代码会导致错误。也许任何人都可以签字?
你可以把线路崩溃吗? – 2012-03-02 12:42:58
我刚刚从控制台添加了发生的errormessage。希望这就是你的意思。 – Marv 2012-03-02 13:03:09
好吧,我越来越近了,我可以看到一段代码,你打电话给SettingsPressed – 2012-03-02 13:04:09