Reputation: 813
(I'm working in Xcode for iOS)
I'm making a word game and when I click Game from the menu it switches to the game view. I want it to automatic display a random word that I inserted. I read I had to put my code in the ViewDidLoad and I did.
However the code was original for the use of a Button then display a random word so I don't know how to edit the code.
here it is:
.h
IBOutlet UILabel *textview;
}
-(IBAction)random;
and the .m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
-(IBAction)random {
int text = rand() % 5;
switch (text) {
case 0:
textview.text = @"BLUE";
break;
case 1:
textview.text = @"GREEN";
break;
case 2:
textview.text = @"RED";
break;
case 3:
textview.text = @"YELLOW";
break;
case 4:
textview.text = @"PINK";
break;
default:
break;
}
}
I hope someone can help me out thanks
Upvotes: 0
Views: 792
Reputation: 4164
Just switch your code from the IBAction to your viewDidLoad - make sure (if you're using IB or storyboarding) that you have a UILabel linked to your code (make sure you synthesize it in the .m) -
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
...then in viewDidLoad -
self.myLabel.text = whateverText;
(You don't have to have it as a property, but it makes the memory management stuff easier)
Also, probably not a good idea to use the name "textview" for a UILabel, as you could easily think it was a UITextView, which is a different animal...
Upvotes: 0
Reputation: 2417
Assuming that everything is part of a view controller, just call your random() method from your viewDidLoad.
Upvotes: 1