发送给UILabel的随机文本 [英] Random Text Sent to UILabel

查看:51
本文介绍了发送给UILabel的随机文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含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的随机文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆