Store UIButton tag value
Need to store the tag from a clicked button:
- (IBAction)buttonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
self.selectedImage = [_images objectAtIndex:button.tag];
}
Works OK.
- (IBAction)buttonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
self.selectedImage开发者_运维知识库 = [_images objectAtIndex:button.tag];
self.selectedTag = button.tag;
}
Gives "makes pointer from integer without cast".
How should I reference button.tag correctly?
A tag
is an NSInteger
, which is just a typedef
for plain old int
. Note that it is not an Object. I can't see what type your self.selectedTag
is, but it seems to be an Object (e.g. NSNumber
). To assign an NSNumber
to selectedTag
, use self.selectedTag = [NSNumber numberWithInteger:button.tag];
Additionally, if you use four blanks at the beginning of each line of code, StackOverflow is going to indent it and use basic syntax highlighting.
精彩评论