加载随机VOID方法

时间:2015-07-21 14:02:50

标签: ios objective-c methods void

我创建了一个内容视图,用于从JSon获取新闻,但我希望随机加载。

我有2种方法:

- (void)radioNews {}
- (void)topNews {}

现在我想在我的viewDidLoad

中获取随机电台新闻或热门新闻
    - (void)viewDidLoad {
           [self radioNews];
    }

这只是加载一个方法是正确的,可以从radioNewstopNews然后在didLoad [self methodResult]告诉getRandom吗?

由于

2 个答案:

答案 0 :(得分:2)

尝试

- (void)viewDidLoad {
    [super viewDidLoad];
    NSInteger random = arc4random()%2;
    if (random == 0) {
        [self radioNews];
    }else{
        [self topNews];
    }
}

答案 1 :(得分:0)

您可以从selector致电NSString 开始使用NSArray

创建NSString
// Define an NSArray of NSString, where a single string is the method name
NSArray *methods = @[@"radioNews", @"topNews"];
// Retrieve a random index
NSUInteger index = arc4random()%methods.count;
// You can use objectAtIndex without check the array bounds because
// the mod operation you did before will always return a value in bounds

// Create a selector instance from a string
SEL randomSelector = NSSelectorFromString([methods objectAtIndex: index]);
// Call the selector
[self performSelector:randomSelector 
           withObject:nil];