更改 UIBarButtonItem 的图像时遇到问题
我正在尝试各种方法来更改 UIBarButtonItem 的图像,一旦按下它,但没有任何运气。
// bookmarkButton is a property linked up in IB
-(IBAction)bookmarkButtonTapped:(id)sender
{
NSLog(@"this action triggers");
// attempt 1
UIBarButtonItem* aBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bookmarkdelete.png"] style:UIBarButtonItemStylePlain target:self action:@selector(bookmarkButtonTapped:)];
bookmarkButton = aBarButtonItem;
[aBarButtonItem release];
// attempt 2
bookmarkButton.image = [UIImage imageNamed:@"bookmarkdelete.png"];
}
还有其他方法可以做到这一点吗?
I'm trying various ways of changing a UIBarButtonItem's image once it has been pressed, without any luck.
// bookmarkButton is a property linked up in IB
-(IBAction)bookmarkButtonTapped:(id)sender
{
NSLog(@"this action triggers");
// attempt 1
UIBarButtonItem* aBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bookmarkdelete.png"] style:UIBarButtonItemStylePlain target:self action:@selector(bookmarkButtonTapped:)];
bookmarkButton = aBarButtonItem;
[aBarButtonItem release];
// attempt 2
bookmarkButton.image = [UIImage imageNamed:@"bookmarkdelete.png"];
}
Is there another way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
工具栏包含一个数组 - items - 作为属性。因此,将工具栏设置为 IBOutlet 属性后,我必须在该数组中插入一个新按钮。如下所示:
The toolbar contains an array - items - as a property. So after setting up the toolbar as an IBOutlet property, I had to insert a new button into that array.. like this:
bookmarkButton 是 UIButton 吗?是否应该通过
(UIButton *)aBarButtonItem.customView
引用而不是直接引用?如果它是 UIButton,那么您需要根据状态设置图像:
- (void)setImage:(UIImage *)image forState:(UIControlState)state
请注意,还有一个
>setBackgroundImage
使用相同的 API(如果您需要的话)。Is bookmarkButton a UIButton? Should it be referenced via
(UIButton *)aBarButtonItem.customView
instead of directly?If it is a UIButton then you'll want to set the image based on state:
- (void)setImage:(UIImage *)image forState:(UIControlState)state
Note that there is also a
setBackgroundImage
with the same API if you want that instead.这应该有效
This should work
尝试
[bookmarkButton setImage:[UIImage imageNamed:@"bookmarkdelete.png"] forState:UIControlStateNormal];
try
[bookmarkButton setImage:[UIImage imageNamed:@"bookmarkdelete.png"] forState:UIControlStateNormal];