发送给UILabel的随机文本

编程入门 行业动态 更新时间:2024-10-27 18:18:09
本文介绍了发送给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的随机文本

本文发布于:2023-11-25 08:48:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629104.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送给   文本   UILabel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!