i’m new to obj-c and i’m trying to code a simple IBAction where a button once clicked change its label. it works good with image background and static string, but when it crashes on this:
int i = (arc4random()%90);
NSString *lab = [NSString stringWithFormat:@"%d", i];
[sender setText:lab];
why? ty in advance
edit: this is the whole method
-(IBAction) toggleUIButtonImage:(id)sender{
NSString *img = @"bulldog-cute-tom-bancroft.jpg";
NSString *imgSel = @"kitten-cute-tom-bancroft.jpg";
UIImage *unselectedImage = [UIImage imageNamed:img];
UIImage *selectedImage = [UIImage imageNamed:imgSel];
if ([sender isSelected]) {
[sender setBackgroundImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:NO];
}else {
[sender setBackgroundImage:selectedImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
int i = (arc4random()%90);
NSString *lab = [NSString stringWithFormat:@"%d", i];
[sender setTitle:lab forState: UIControlStateNormal];
}
It’s not the random int or the int2str conversion. It’s the way you set the text of your button.
Use this and it should work:
(That is if sender actually is a UIButton of course)
EDIT:
A cast from sender to UIButton* (or any other specific class you want to make respond to the method) is recommended for this as well to ensure you work with the right classmethods.