2012-04-11 139 views
2

我有一個導航控制器,其中包含一些按鈕的視圖,但是當我按下按鈕時,我得到一個EXC_BAD_ACCESS錯誤。因爲目標設置正確,我不能認爲我做錯了。它崩潰了按鈕是以編程方式添加還是通過IB添加。UIButton崩潰的應用程序觸摸

按鈕代碼:

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 incase,以任何方式幫助。

- (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; 
} 
+0

嘗試放置斷點並讓我們知道它崩潰的位置 – 2012-04-11 15:33:04

+0

我應該在哪裏放置它們?該應用程序崩潰時按下按鈕,其餘的代碼只是添加各種其他UI的東西子視圖。 – NinjaLikesCheez 2012-04-11 15:35:28

回答

1

我也遇到這個錯誤;我一直在使用SpriteKit進行編程,並以某種方式實現我的按鈕,然後當我回到不使用框架時,突然間,我實現的每個按鈕都會導致訪問錯誤。

我發現我正在初始化viewDidLoad中的按鈕,因爲我一直在使用didMoveToView,而且我不小心把它們當作了相同的函數(這實際上我並沒有真正掌握什麼viewDidLoad一樣)。

但我這樣做的時候:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) 
{ 
    // Custom initialization 
    [self addButton]; 

} 
return self; 
} 


- (void)addButton 
{ 
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45, 45)]; 

[button setTitle:@"CLICK" forState:UIControlStateNormal]; 


[button addTarget:self action:@selector(buttonPressed:) 
forControlEvents:UIControlEventTouchDown]; 

[self.view addSubview:button]; 
} 

- (void)buttonPressed:(UIButton *)button 
{ 
    NSLog(@"WORKED"); 
} 

一切工作正常...因此,嘗試的是,如果你仍然有問題。 祝你好運!

+0

謝謝,這是一段時間以前,我不記得我做了什麼使它工作! – NinjaLikesCheez 2013-11-11 16:06:56

4

您的代碼中沒有任何內容似乎指向任何問題。 Use this tutorial使Xcode在所有例外情況下中斷。這會讓你更接近'犯罪現場',而不是撞上主力。

+0

現在就做,謝謝! – NinjaLikesCheez 2012-04-11 15:42:05