发送到 UILabel 的随机文本

发布于 2024-10-15 21:57:42 字数 123 浏览 4 评论 0原文

我正在尝试创建一个包含 x 数量的文字字符串的数组,该数组会在您点击 UIButton 后随机发送一个到 UILabel。

我应该如何构建 .h 和 .m 文件来做到这一点?另外,生成我需要的随机数的最佳方法是什么?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜清冷一曲。 2024-10-22 21:57:42

您将获得索引的随机整数,然后将获得的对象传递给 UILabel 的文本属性,例如:

//assuming you already have an NSArray of strings
myLabel.text = [arrayOfString objectAtIndex:arc4random() % [arrayOfString count]];

您可以将上述代码放入按钮按下时调用的方法中。

编辑: 根据要求,这是一个简单的 Xcode 项目

(注意:由于它是随机的,因此您有可能会得到相同的文本,因此可能看起来文本没有更改,但它确实更改为与之前您看不到的相同文本)

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.

EDIT: As requested here's a simple Xcode project.

(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)

暗藏城府 2024-10-22 21:57:42
- (void) applicationDidFinishLaunching:(UIApplication*)application 
{   
    //Create window
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [_window makeKeyAndVisible];
    sampleArray = [[NSArray arrayWithObjects: @"iPhone", @"iPod", @"iMac", @"Newton",@"iPad",@"Lisa",@"Mac mini",@"Delta" ,nil] retain];
    randButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    [randButton setFrame:CGRectMake(20.0f, 30.0f, 60.0f, 40.0f)];
    [randButton setTitle:@"Random" forState:UIControlStateNormal];
    [randButton addTarget:self action:@selector(randNumberGenerate) forControlEvents:UIControlEventTouchUpInside];
    [_window addSubview:randButton];    
}


- (void) randNumberGenerate
{
    NSString* string = [sampleArray objectAtIndex:(arc4random()%[sampleArray count])];
    UILabel* displayRandNum = [[UILabel alloc] initWithFrame:CGRectMake(120.0f, 30.0f, 80.0f, 30.0f)];
    displayRandNum.text = string;
    [_window addSubview:displayRandNum];
    [displayRandNum release];
}
- (void) applicationDidFinishLaunching:(UIApplication*)application 
{   
    //Create window
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [_window makeKeyAndVisible];
    sampleArray = [[NSArray arrayWithObjects: @"iPhone", @"iPod", @"iMac", @"Newton",@"iPad",@"Lisa",@"Mac mini",@"Delta" ,nil] retain];
    randButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    [randButton setFrame:CGRectMake(20.0f, 30.0f, 60.0f, 40.0f)];
    [randButton setTitle:@"Random" forState:UIControlStateNormal];
    [randButton addTarget:self action:@selector(randNumberGenerate) forControlEvents:UIControlEventTouchUpInside];
    [_window addSubview:randButton];    
}


- (void) randNumberGenerate
{
    NSString* string = [sampleArray objectAtIndex:(arc4random()%[sampleArray count])];
    UILabel* displayRandNum = [[UILabel alloc] initWithFrame:CGRectMake(120.0f, 30.0f, 80.0f, 30.0f)];
    displayRandNum.text = string;
    [_window addSubview:displayRandNum];
    [displayRandNum release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文