I'm trying various ways of changing a UIBarButtonItem's image once it has been pressed, without any luck.
我正在尝试各种方法来改变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"];
}
Is there another way to do this?
还有别的办法吗?
4 个解决方案
#1
2
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:
工具栏包含一个数组—项—作为属性。因此,在将工具栏设置为IBOutlet属性之后,我必须向该数组插入一个新按钮。是这样的:
NSMutableArray *items = [[NSMutableArray alloc] initWithArray:self.toolBar.items];
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"newButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonTapped:)];
[items replaceObjectAtIndex:0 withObject:newButton];
self.toolBar.items = items;
[newButton release];
[items release];
#2
0
Is bookmarkButton a UIButton? Should it be referenced via (UIButton *)aBarButtonItem.customView
instead of directly?
bookmarkButton UIButton吗?应该通过(UIButton *)aBarButtonItem引用它。customView直接代替吗?
If it is a UIButton then you'll want to set the image based on state: - (void)setImage:(UIImage *)image forState:(UIControlState)state
如果它是一个UIButton,那么您将希望基于state: - (void)setImage:(UIImage *)image forState:(UIControlState)状态设置图像
Note that there is also a setBackgroundImage
with the same API if you want that instead.
请注意,如果您希望使用相同的API,那么还有一个setBackgroundImage。
#3
0
This should work
这应该工作
UIImage *normalButtonImage = [UIImage imageNamed:@"TableViewIcon"];
UIImage *selectedButtonImage = [UIImage imageNamed:@"CollectionViewIcon"];
CGRect rightButtonFrame = CGRectMake(0, 0, normalButtonImage.size.width,
normalButtonImage.size.height);
UIButton *rightButton = [[UIButton alloc] initWithFrame:rightButtonFrame];
[rightButton setBackgroundImage:normalButtonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:selectedButtonImage forState:UIControlStateSelected];
[rightButton addTarget:self action:@selector(toggleTableView:)
forControlEvents:UIControlEventTouchDown];
self.toggleMediaView = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
[self.navigationItem setLeftBarButtonItem:self.toggleMediaView];
self.navigationItem.leftBarButtonItem.enabled = NO;
#4
-3
try [bookmarkButton setImage:[UIImage imageNamed:@"bookmarkdelete.png"] forState:UIControlStateNormal];
试(bookmarkButton setImage:(界面图像imageNamed:@”bookmarkdelete。png”]forState UIControlStateNormal):;
#1
2
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:
工具栏包含一个数组—项—作为属性。因此,在将工具栏设置为IBOutlet属性之后,我必须向该数组插入一个新按钮。是这样的:
NSMutableArray *items = [[NSMutableArray alloc] initWithArray:self.toolBar.items];
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"newButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonTapped:)];
[items replaceObjectAtIndex:0 withObject:newButton];
self.toolBar.items = items;
[newButton release];
[items release];
#2
0
Is bookmarkButton a UIButton? Should it be referenced via (UIButton *)aBarButtonItem.customView
instead of directly?
bookmarkButton UIButton吗?应该通过(UIButton *)aBarButtonItem引用它。customView直接代替吗?
If it is a UIButton then you'll want to set the image based on state: - (void)setImage:(UIImage *)image forState:(UIControlState)state
如果它是一个UIButton,那么您将希望基于state: - (void)setImage:(UIImage *)image forState:(UIControlState)状态设置图像
Note that there is also a setBackgroundImage
with the same API if you want that instead.
请注意,如果您希望使用相同的API,那么还有一个setBackgroundImage。
#3
0
This should work
这应该工作
UIImage *normalButtonImage = [UIImage imageNamed:@"TableViewIcon"];
UIImage *selectedButtonImage = [UIImage imageNamed:@"CollectionViewIcon"];
CGRect rightButtonFrame = CGRectMake(0, 0, normalButtonImage.size.width,
normalButtonImage.size.height);
UIButton *rightButton = [[UIButton alloc] initWithFrame:rightButtonFrame];
[rightButton setBackgroundImage:normalButtonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:selectedButtonImage forState:UIControlStateSelected];
[rightButton addTarget:self action:@selector(toggleTableView:)
forControlEvents:UIControlEventTouchDown];
self.toggleMediaView = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
[self.navigationItem setLeftBarButtonItem:self.toggleMediaView];
self.navigationItem.leftBarButtonItem.enabled = NO;
#4
-3
try [bookmarkButton setImage:[UIImage imageNamed:@"bookmarkdelete.png"] forState:UIControlStateNormal];
试(bookmarkButton setImage:(界面图像imageNamed:@”bookmarkdelete。png”]forState UIControlStateNormal):;