我有一个导航 Controller ,它包含一些按钮的 View ,但是当我按下按钮时,我收到 EXC_BAD_ACCESS 错误。我不认为自己做错了什么,因为目标设定是正确的。无论按钮是通过编程方式添加还是通过 IB 添加,它都会崩溃。
按钮代码:
UIButton *reportsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
reportsButton.frame = CGRectMake(20, 100, 100, 50);
[reportsButton setTitle:@"Reports" forState:UIControlStateNormal];
[reportsButton addTarget:self action:@selector(reportsButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
功能按钮正在尝试访问:
- (void)reportsButtonTouched:(id)sender
{
NSLog(@"working");
}
错误:
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); //EXC_BAD_ACCESS code=1
}
按钮尝试访问的功能存在。
也许这与我不知道的 NavigationController 的工作方式有关,但我以前已经这样做过,没有任何问题。
感谢您的回答,我真的很感谢我之前从这个网站获得的帮助。
编辑:这是我的 AppDelegates didFinishLaunching,以防有任何帮助。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *homevc = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:homevc];
[self.window addSubview:nav.view];
[self.window makeKeyAndVisible];
return YES;
}
最佳答案
您的代码中似乎没有任何内容表明存在任何问题。 Use this tutorial使 Xcode 在所有异常情况下中断。这会让你更接近“犯罪现场”,而不是直接撞到主线。
关于iphone - UIButton 触摸时导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10109096/