【问题标题】:Random View Controller随机视图控制器
【发布时间】:2014-11-28 20:43:52
【问题描述】:

我有 26 个不同的视图控制器。当我单击按钮时,我想显示随机视图控制器。我也有每个没有 .xlb 文件的视图控制器的类。这个方法我试过了,还是不行。

NSString *randomViewControllerClassName = viewControllerList[arc4random() % 99];
UIViewController *randomVC = [[NSClassFromString(randomViewControllerClassName) alloc] init]
[self presentViewController:randomVC animated:YES completion:nil];

viewControllerList 由我的 26 个视图控制器组成。

【问题讨论】:

  • %99?你说你只有26个vc的吧?
  • 你是什么意思,“它没有工作”?你得到了什么结果?具体一点。
  • 如果你只有26个vc,为什么要使用'%99'??

标签: ios iphone xcode ipad


【解决方案1】:

您可以将视图控制器打包到一个数组中,然后掷骰子以供数组索引使用。

也许是这样的:

UIViewController *randomVC = viewControllerList[arc4random() % [viewControllerList count]];
[self presentViewController:randomVC animated:YES completion:nil];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多