0

So right now, I have a right bar button in storyboard hooked up with a segue to another view controller. All the functions work fine here. (without code, all done through storyboard)

Now, I'm connecting to a webAPI call to see if the image needs to be changed. I have the code below:

 if ( json.count == 0) {
   self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"notification.png"]]];
}
else
{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"notification-new.png"]]];
}

So this all work fine and the image does change if there are more than 1 json data received. However, the button itself does not push to the new view controller

user3178926
  • 339
  • 1
  • 5
  • 15

2 Answers2

0

You have created custom button. So now you have to set UIBarButtonItem s properties yourself

@property(nonatomic)         SEL   action;     // default is NULL
@property(nonatomic,assign)  id    target;     // default is nil

I can not check it right now but probably following should work:

UIBarButtonItem *originalRightButton = self.navigationItem.rightBarButtonItem;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"notification-new.png"]]];
self.navigationItem.rightBarButtonItem.target = originalRightButton.target;
self.navigationItem.rightBarButtonItem.action = originalRightButton.action;
Avt
  • 16,927
  • 4
  • 52
  • 72
  • Hi, I have implemented what you did. It is not working but I think it's because I'm not understanding the action and target concept. – user3178926 Mar 13 '14 at 18:03
  • my original button is called goNotifications, declared as a property in my .h file. so I implemented the following code under this comment. – user3178926 Mar 13 '14 at 18:04
  • self.goNotifications = self.navigationItem.rightBarButtonItem; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"notification-new.png"]]]; self.navigationItem.rightBarButtonItem.target = self.goNotifications.target; self.navigationItem.rightBarButtonItem.action = self.goNotifications.action; – user3178926 Mar 13 '14 at 18:05
0

You can set the custom button along with its action and selector

UIImage* image = [UIImage imageNamed:@"notification.png"];
UIButton *someButton = [[UIButton alloc] initWithImage:image];
[someButton setBackgroundImage:image forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(method) 
                 forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *rightBtn =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=rightBtn;

In the button's target you can call

[self performSegueWithIdentifier:@"yourSegueName" sender:self];. 

The segue has to be defined in storyboard with a name.

Hope this helps.

Harsh
  • 2,852
  • 1
  • 13
  • 27
user2071152
  • 1,161
  • 1
  • 11
  • 25