我正在尝试创建一个包含x个文字字符串的数组,这些数组将在您按下UIButton后随机发送一个到UILabel.
I'm trying to create an array that holds x amount of literal strings that will randomly send one to a UILabel after you hit a UIButton.
我应该如何构造.h和.m文件来做到这一点?另外,生成我需要的随机数的最佳方法是什么?
How should I structure the .h and .m files to do this? Also, what is the best way to generate the random number I need?
推荐答案您将获得一个随机整数作为索引,然后将获得的对象传递给UILabel的text属性,例如:
You would get a random integer for the index, and then just pass the object you get to the UILabel's text property, eg:
//assuming you already have an NSArray of strings myLabel.text = [arrayOfString objectAtIndex:arc4random() % [arrayOfString count]];您已将上面的代码放入按下按钮时调用的方法中.
You'd put the above code in the method that the button calls when it's pressed.
根据要求,这是一个简单的Xcode项目.
(注意:由于随机性,您有可能会得到相同的文本,因此该文本似乎没有变化,但确实有所变化,但变为与您之前看不到的文本相同的文本)
(NOTE: As it's random there's a chance that you'll get the same text so it may appear that the text doesn't change, it does but it changes to the same text as before which you don't see)
更多推荐
发送给UILabel的随机文本
发布评论