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

标签 ios button exc-bad-access sigabrt navigationcontroller

我是 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/

相关文章:

ios - 如何在将 JSON 数据流式传输到 NSURLConnection 时处理分块

ios - iOS 中的 react 原生方向

ios - 将 UINavigationBar 更改为透明色

javascript - 使用 jQuery 通过复选框禁用/启用按钮

css - 过渡前加载样式?

iphone - 如何将 NSDate 的本地化日期作为字符串获取?

JavaFX从另一个线程访问gui按钮操作

macos - EXC_BAD_ACCESS 当我关闭窗口时,这也是我的应用程序的委托(delegate)

从互联网下载资源时 iOS 应用程序崩溃

objective-c - C 一般保护错误试图访问数组中的对象