如何更改“JUST”导航右侧栏按钮图像?

时间:2014-03-13 07:28:00

标签: ios uinavigationcontroller storyboard segue rightbarbuttonitem

现在,我在故事板中有一个正确的条形按钮,与另一个视图控制器相连。所有功能在这里都可以正常工作(没有代码,都通过故事板完成)

现在,我正在连接到webAPI调用以查看是否需要更改图像。我有以下代码:

 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"]]];
}

所以这一切都很好,如果收到的json数据超过1个,图像确实会改变。但是,按钮本身不会推送到新的视图控制器

2 个答案:

答案 0 :(得分:0)

您已创建自定义按钮。所以现在你必须自己设置UIBarButtonItem属性

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

我现在无法检查,但可能以下工作:

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;

答案 1 :(得分:0)

您可以设置自定义按钮及其操作和选择器

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;

在按钮的目标中,您可以拨打

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

必须在故事板中使用名称定义segue。

希望这有帮助。