Reputation:
I'm new to Objective-C.
I created several SingleView
's that display different content. I now need to create some code that will allow the user, when he presses the next button to get to a different, randomly picked, View
. How can I do this?
Upvotes: 1
Views: 65
Reputation: 62676
Or, for fingers tired of typing:
NSArray *viewControllers = @[vc1, vc2, vc3];
UIViewController *randomViewController = viewControllers[arc4random_uniform(viewControllers.count)];
Upvotes: 1
Reputation: 11696
Make sure you have imported all your View Controllers.
#import "ViewController0.h"
#import "ViewController1.h"
#import "ViewController2.h"
and so on. Then use this code for your button:
-(IBAction)myButtonMethod:(id)sender {
NSInteger randomNumber = arc4random() % 3; // generates a random number between 0 and 2
switch (randomNumber) {
case 0:
ViewController0 *vc = [[ViewController0 alloc] init];
[[self navigationController] pushViewController:vc animated:YES];
break;
case 1:
ViewController1 *vc = [[ViewController1 alloc] init];
[[self navigationController] pushViewController:vc animated:YES];
break;
case 2:
ViewController2 *vc = [[ViewController2 alloc] init];
[[self navigationController] pushViewController:vc animated:YES];
break;
default:
break;
}
}
Modify the code to suit the number of VCs you want to include.
Upvotes: 0