正常的按钮,你可以做如何使UIBarButton工作作为开关鉴于UIBarButton没有选定的属性
btn.selected =!btn.selected。
我会如何用UIBarButton来解决这个问题?
正常的按钮,你可以做如何使UIBarButton工作作为开关鉴于UIBarButton没有选定的属性
btn.selected =!btn.selected。
我会如何用UIBarButton来解决这个问题?
您可以用自定义视图
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:normalImage forState:UIControlStateNormal];
[button setImage:selectedImage forState:UIControlStateSelected];
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
然后
-(void)btnClick:(id)sender{
UIBarButtonItem *item = (UIBarButtonItem *)sender;
UIButton *button = (UIButton *)item.customView;
[button setIsSelected:YES];
}
想法1.如果您在IB中创建界面,请尝试将标准UIButton对象拖入UIToolbar。这应该将UIButton封装在UIBarButton中。 将UIBarButton对象设置为普通。像往常一样创建网点。 然后,您可以使用标准按钮以及所有可爱的标准事物,例如工具栏中的选定状态。
想法2.我几乎放弃了使用工具栏,因为控件不灵活,正如你找到的。现在我只需将UIToolbar的实例替换为UIViews,并执行少量代码以管理旋转,调整大小等等......我发现它更加灵活,让我更容易地包含其他控件。
你可以在UIBarItem里面使用一个自定义的按钮,保持一个按钮就可以了。
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:self.yourButton]
[self.navigationItem setRightBarButtonItem:barButton];
然后:
self.yourButton.select = !self.yourButton.select;
它是不可能有通过的UIBarButtonItem选定的属性创建的UIBarButtonItem马上。但是您可以通过放置UIButton并将其定制为UIBarbutton来实现此目的。这个链接对你更有用... How do I programmatically get the state of UIBarButtonItems?
UIBarButtonItem * add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewRow :)]; (void)addNewRow:(UIButton *)sender(or) - (void)addNewRow:(UIBarButton *)sender .... both will work – 2013-03-19 09:05:29