随机NSMutableArray生成器

时间:2014-07-04 18:05:40

标签: ios objective-c nsmutablearray nsarray

这对你来说可能是非常基础,但我对Objective-C和Xcode来说真的很陌生,我正在迷失方向。

我想对NSMutableArray测试随机事实生成器(仅用于学习目的)。

我的问题是如何将我的NSMutableArray连接到用户界面中的Button?我知道这两个步骤之间缺少相关链接(创建NSMutableArray并将其连接到Button

这是我在.m文件中的代码:

-(void)randomButton {
NSMutableArray *randomFacts = [[NSMutableArray alloc]initWithObjects:@"fact1", @"fact2", @"fact3", @"fact4", @"fact5"];
NSInteger randomIndex = arc4random()%randomFacts.count;
self.label.text = randomFacts[randomIndex];
}

我正在网上搜索,但没有任何内容。

谢谢

1 个答案:

答案 0 :(得分:-1)

在不知道您的代码的情况下帮助您并不容易。但这应该涵盖你需要的大部分内容:

-(void)buttonPressed{
    NSArray *textArray = @[ @"text1", @"text2", @"text3", @"text4", @"text5" ];
    NSInteger randomIndex = arc4random()%textArray.count;
    self.label.text = textArray[randomIndex];
}

您可以按照本教程设置用户界面,然后将各个部分放在一起:http://codewithchris.com/xcode-tutorial-user-interaction/