我创建了一个内容视图,用于从JSon
获取新闻,但我希望随机加载。
我有2种方法:
- (void)radioNews {}
- (void)topNews {}
现在我想在我的viewDidLoad
- (void)viewDidLoad {
[self radioNews];
}
这只是加载一个方法是正确的,可以从radioNews
和topNews
然后在didLoad [self methodResult]
告诉getRandom吗?
由于
答案 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];