我是 Objective-C 的新手,目前正在开发 iOS 应用程序。 我有一个名为 SettingsButton 的按钮,它位于自定义 UIView 对象内。 当我按下此按钮时,将调用 FileOwner“ViewController.m”的 Touch Up Inside 处理程序,该处理程序稍后将在 NavigationController 上推送 ViewController。 但应用程序因 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!");
}
澄清一下:我的目的只是在按下“设置按钮”并使用“后退按钮”返回时更改 View 。但应用程序已经因空事件处理程序而崩溃。
错误消息当 sigabrt 发生时:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString SettingsPressed:]: unrecognized selector sent to instance 0x7a06fc0'
最佳答案
我刚刚解决了这个问题。 我必须在 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;
}
这么小的错误却造成这么大的影响。 但真的不知道为什么我上面的代码会导致错误。也许有人可以澄清一下?
关于使用 NavigationController 按下按钮时 iOS 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9533409/