EXEC_BAD_ACCESS在IBAction的iOS5中

问题描述:

我已经写了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

第一个变化这段代码工作到这个

- (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

你必须为你的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

我猜他还需要为该按钮添加数据成员。我的意思是代码应该看起来像这样。

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

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

,并尝试在.m文件与

@synthesize button; 

更换

@synthesize button = _button; 

我发现我的问题的解决方案。

主要问题是,如果我在本地创建视图控制器对象(即,在任何方法内)并将其添加为子视图,那么当您调用任何IBAction时,此时它将引发异常,因为内存因为viewController在本地声明时会自动解除分配。

+0

这实际上帮助我追踪我的问题,这是我的视图控制器被垃圾收集,我不得不使它变成一个强大的变量。 – box86rowh 2012-09-21 15:12:43