加载随机 VOID 方法

Load Random VOID method

我已经为从 JSon 获取新闻创建了一个内容视图,但我想随机加载。

我有两种方法:

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

现在我想在我的 viewDidLoad

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

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

谢谢

尝试

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

您可以从 NSString 调用 selector 开始用 NSString

创建一个 NSArray
// 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];