0

I create NavigationItem and a BarButtonItem use items.leftBarButtonItem to add to ,and the baritem's action i use @selector to deal with ,it can complie fine but when i touch the buttonitem ,it's crashed ..maybe the selector cause it . when i close ARC on config file ,it turns right ...why ? i was confused with ARC,anyone could tell me how to deal with it ?

@implementation IGMainViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 340, 44)];

    UINavigationItem  *items = [[UINavigationItem alloc] init];
    UIBarButtonItem  *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"aa" style:UIBarButtonSystemItemAdd target:self action:@selector(doItAgain:)];
    items.leftBarButtonItem = leftBtn;
    [navBar pushNavigationItem:items animated:NO];
    [self.view addSubview:navBar];
}
-(void)doItAgain:(id)sender
{
    NSLog(@"sss");
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

//fixed version:

    //old:
    IGMainViewController *mview = [[IGMainViewController alloc] init];
    [self.view addSubview:mview.view];
    //fixed:
    @property(strong,nonatomic) IGMainViewController *mview;
igaves
  • 63
  • 1
  • 6
  • EXC_BAD_ACESS usually means that something is trying to call a method of an object thats not in memory anymore. enable nszombie (http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-in-xcode), which tells you when this happens, and see what it logs when it crashes – Ahti Sep 20 '12 at 01:55
  • i found out that my delegat file's main view controller wasy deallocated ,i use local arguments for addSubview :..thx for all . – igaves Sep 20 '12 at 02:29
  • please don't accept a wrong answer. answer your own question with the thing you found out and accept that, otherwise it could mislead others seeking for similar questions – Ahti Sep 20 '12 at 03:02

1 Answers1

2

@selector(doItAgain) means that doItAgain methods takes no parameters, while your method takes 1. To fix the issue add colon to the selector name: @selector(doItAgain:)

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • 1
    It's a colon, not a semicolon. Also, it's probably worth pointing out that `doItAgain` and `doItAgain:` are completely different and unrelated selectors as far as ObjC is concerned. But otherwise, this looks like the right answer. – abarnert Sep 20 '12 at 00:24
  • this is a good thing to point out, but it would cause a unrecognized message exception, no bad access. – Ahti Sep 20 '12 at 01:50