Reputation: 27
i was playing around with an idea. in a single lable in a view when the view appears a random number is displayed. presently i have aview, a lable and a button. press button random number is generated and the lables title is replaced with a random number....random number displayed.
what i wish to acheive is that there is no button, just a view and a text lable and when the view appears a random number is displayed also instantaneously with out the need for a button to prompt generation. here isi the code i have so far
#import "Mainview.h"
@implementation Mainview
- (IBAction)pushstart {
NSString *title = nil;
NSString *path = nil;
int Number = arc4random() % 12;
switch(Number) {
case 0:
namel.text = @"1";
break;
case 1:
namel.text = @"2";
break;
case 2:
namel.text = @"3";
break;
case 3:
namel.text = @"4";
break;
case 4:
namel.text = @"5";
break;
case 5:
namel.text = @"6";
break;
case 6:
namel.text = @"7";
break;
case 7:
namel.text = @"8";
break;
case 8:
namel.text = @"9";
break;
case 9:
namel.text = @"10";
break;
case 10:
namel.text = @"11";
break;
case 11:
namel.text = @"12";
break;
break;
default:
break;
}
}
@end
Upvotes: 0
Views: 2716
Reputation: 921
You can use arc4random_uniform
to generate the random number.
- (IBAction)pushstart {
namel.text = arc4random_uniform(12) + 1
}
Hope this can help.
Upvotes: 0
Reputation: 4729
Here is the way shorter answer, this method goes in whatever class is controlling the view (same place as the current action method):
- (void) awakeFromNib {
srandom(time(NULL));
[namel setIntValue: random %12];
}
Upvotes: 3