2012-02-02 70 views
0

我已經寫了iOS5的xcode 4.2中按鈕點擊操作的示例代碼。EXEC_BAD_ACCESS在IBAction的iOS5中

下面是代碼

.H

#import <UIKit/UIKit.h> 

@interface FirstViewController : UIViewController 


@property(strong,nonatomic) IBOutlet UIButton *button; 
-(IBAction)changed; 
@end 

.M

#import "FirstViewController.h" 

@implementation FirstViewController 

@synthesize button=_button; 

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

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 

    [_button addTarget:self action:@selector(changed)  forControlEvents:UIControlEventTouchUpInside]; 
[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
} 
-(IBAction)changed 
{ 
    NSLog(@"clicked"); 

    } 
    - (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return YES; 
} 

@end 

但是,當我按一下按鈕。我得到例外。如何解決這個問題?同樣是在的iOS 4.3

+0

顯示崩潰日誌並在應用程序崩潰.. – Sarah 2012-02-02 06:07:29

+0

將您的來電超到你的'viewDidLoad'方法 – Rog 2012-02-02 06:09:30

+0

頂部如果我點擊按鈕的聲明,在main.m文件 – Bharath 2012-02-02 06:22:28

回答

1

第一個變化這段代碼工作到這個

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
    [_button addTarget:self action:@selector(changed) forControlEvents:UIControlEventTouchUpInside]; 
} 
+0

不,這不起作用 – Bharath 2012-02-02 06:22:01

+0

下面是該鏈接EXCEPTION http://i.stack.imgur.com/mIcp7.png – Bharath 2012-02-02 06:35:42

+0

@Bharath在xcode中添加異常斷點 – iNoob 2012-02-02 06:39:04

-1

你必須爲你的changed行動的錯誤消息簽名。它應該是:

- (IBAction)changed:(id)sender 

,並在addTarget行代碼:

@selector(changed:) 

PS:爲什麼您使用的_button?我不認爲這與問題有關,但您應該使用self.button來代替。應該避免直接訪問實例變量,特別是在這種情況下,您允許編譯器決定變量應該具有的名稱。

PPS:正如@InderKumarRathore所述,在運行自己的代碼之前,您還應該調用[super viewDidLoad]

+0

嗯..我做了所有更改你說。相同的概率。 我改變我的方法名稱 - (IBAction)更改:(id)發件人 和目標到@選擇器(更改:), _button self.button。即使這樣。沒有變化 – Bharath 2012-02-02 06:33:22

+0

這是這個EXCEPTION的鏈接 http://i.stack.imgur.com/mIcp7.png – Bharath 2012-02-02 06:36:00

+0

我不確定。我懷疑實際的錯誤是在你的代碼中的其他地方。 EXEC_BAD_ACCESS通常是內存管理錯誤,您是否啓用了ARC? – 2012-02-02 06:36:43

0

我猜他還需要爲該按鈕添加數據成員。我的意思是代碼應該看起來像這樣。

@interface FirstViewController : UIViewController{ 
    IBOutlet *UIButton *button; 
} 

@property(strong,nonatomic) IBOutlet UIButton *button; 
-(IBAction)changed; 
@end 

,並嘗試在.m文件與

@synthesize button; 

更換

@synthesize button = _button; 

1

我發現我的問題的解決方案。

主要問題是,如果我在本地創建視圖控制器對象(即,在任何方法內)並將其添加爲子視圖,那麼當您調用任何IBAction時,此時它將引發異常,因爲內存因爲viewController在本地聲明時會自動解除分配。

+0

這實際上幫助我追蹤我的問題,這是我的視圖控制器被垃圾收集,我不得不使它變成一個強大的變量。 – box86rowh 2012-09-21 15:12:43