Chris Truman
Chris Truman

Reputation: 903

UIBarButtonItem not firing selector after it has been shown twice

I use this category method on UIBarButtonItem to create custom buttons as follows:

+ (UIBarButtonItem*)itemWithImage:(UIImage*)image forState:(UIControlState)controlState target:(id)target action:(SEL)action{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:controlState];

    button.frame= CGRectMake(0.0, 0.0, 44, 44);
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 44, 44) ];
    [v addSubview:button];

    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

I then create the buttons and assign them to the navigation item in my view controller as follows:

-(void)viewDidLoad{
    [super viewDidLoad]; 
    UIBarButtonItem * cancelButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"Cancel"] forState:UIControlStateNormal target:self action:@selector(cancel)];
    self.navigationItem.leftBarButtonItem = cancelButtonItem;

    UIBarButtonItem * checkmarkButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"checkmark_active"] forState:UIControlStateNormal target:self action:@selector(done)];
    self.navigationItem.rightBarButtonItem = checkmarkButtonItem;
}

The first time I create a view controller and push it, the button works, but when creating a brand new view controller and pushing it onto the navigation stack, it breaks. Any ideas? I have thoroughly debugged this and am out of ideas.

Upvotes: 0

Views: 145

Answers (1)

Peeks
Peeks

Reputation: 151

There doesn't appear to be anything wrong with the category section of your code. I suspect that the error might be stemming from something else. Some more information or code may help here... what do you mean by break etc. Check the way you initialise the ViewController before you push it onto the stack, perhaps a simple syntax error or mistaken nib name if using outlets.

Upvotes: 1

Related Questions