0

嗨,我正在创建一个应用程序,当我按下按钮时,我有代码,它工作正常,但是当我运行应用程序并按下它显示的按钮时,我想更改一些东西:

“好吧,嗨,哟,哟,你好,好吧,嗨,哟,哟,嗨,你好,你好,好吧”

那是前 13 次点击,这就是我每次打开应用程序时的顺序。基本上我不希望他们连续重复两次,而且我希望他们在我启动应用程序时以不同的顺序启动。

还有 1 件事我希望能够写至少 2 行文本,但我如何使用标签来做到这一点?

这是我的代码:

。H

@interface ViewController1 : UIViewController  {

    IBOutlet UILabel *textview;

}

-(IBAction)random;

.m

@接口 ViewController1 ()

@结尾

@实现视图控制器1

-(IBAction)随机{

int text = rand() % 5;

switch (text) {

    case 0:

        textview.text = @"Hello";

        break;

    case 1:

        textview.text = @"hi";

        break;

    case 2:

        textview.text = @"alright";

        break;

    case 3:

        textview.text = @"yoo";

        break;

    case 4:

        textview.text = @"hiya";

        break;

    default:

        break;

}

}

谢谢你 :)

4

2 回答 2

1

使用函数arc4random()而不是random(). 您面临的问题是因为该函数rand需要在调用之前设置种子。这是后台使用的随机数生成器的起始值rand。当你不使用自己的种子时,它总是相同的默认值,因此你总是得到相同的随机值序列。使用arc4random时无需播种。有关详细信息,请参阅此博客文章文档

#include <stdlib.h>
...
int text = arc4random() % 5;
于 2012-10-11T12:10:26.163 回答
0
@interface ViewController1 ()

@end

@implementation ViewController1



-(IBAction)random {

    // Pseudocode here
    if (srand() not yet called) then
      srand();
    endif
    // end Pseudocode

    // You are better to put the call to srand() somewhere 
    // it will only ever be called once, rather than having
    // to mess around with an if-statement.

    int text = arc4random() % 50;

    switch (text) {

        case 0:

            textview.text = @"My text here";

            break;

   default:

            break;

    }

}
于 2012-10-18T08:55:31.360 回答