如何显示随机视图?

时间:2014-02-23 15:07:35

标签: iphone random view

我是Objective-C的新手。 我创建了几个显示不同内容的SingleView。我现在需要创建一些允许用户的代码,当他按下下一个按钮以获得另一个随机选择的View时。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

或者,对于厌倦打字的手指:

NSArray *viewControllers = @[vc1, vc2, vc3];
UIViewController *randomViewController = viewControllers[arc4random_uniform(viewControllers.count)];

答案 1 :(得分:0)

确保已导入所有视图控制器。

#import "ViewController0.h"
#import "ViewController1.h"
#import "ViewController2.h"

等等。然后将此代码用于您的按钮:

-(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;
    }
}

修改代码以适合您想要包含的VC数量。