在 xcode 的视图中生成单个随机数
我正在考虑一个想法。在视图中的单个标签中,当视图出现时,会显示随机数。目前我有一个视图、一个标签和一个按钮。按下按钮会生成随机数,并且标签标题将替换为随机数......显示随机数。
我希望实现的是没有按钮,只有一个视图和一个文本标签,当视图出现时,也会立即显示一个随机数,而不需要按钮来提示生成。 这是我到目前为止的代码
#import "Mainview.h"
@implementation Mainview
- (IBAction)pushstart {
NSString *title = nil;
NSString *path = nil;
int Number = arc4random() % 12;
switch(Number) {
case 0:
namel.text = @"1";
break;
case 1:
namel.text = @"2";
break;
case 2:
namel.text = @"3";
break;
case 3:
namel.text = @"4";
break;
case 4:
namel.text = @"5";
break;
case 5:
namel.text = @"6";
break;
case 6:
namel.text = @"7";
break;
case 7:
namel.text = @"8";
break;
case 8:
namel.text = @"9";
break;
case 9:
namel.text = @"10";
break;
case 10:
namel.text = @"11";
break;
case 11:
namel.text = @"12";
break;
break;
default:
break;
}
}
@end
i was playing around with an idea. in a single lable in a view when the view appears a random number is displayed. presently i have aview, a lable and a button. press button random number is generated and the lables title is replaced with a random number....random number displayed.
what i wish to acheive is that there is no button, just a view and a text lable and when the view appears a random number is displayed also instantaneously with out the need for a button to prompt generation.
here isi the code i have so far
#import "Mainview.h"
@implementation Mainview
- (IBAction)pushstart {
NSString *title = nil;
NSString *path = nil;
int Number = arc4random() % 12;
switch(Number) {
case 0:
namel.text = @"1";
break;
case 1:
namel.text = @"2";
break;
case 2:
namel.text = @"3";
break;
case 3:
namel.text = @"4";
break;
case 4:
namel.text = @"5";
break;
case 5:
namel.text = @"6";
break;
case 6:
namel.text = @"7";
break;
case 7:
namel.text = @"8";
break;
case 8:
namel.text = @"9";
break;
case 9:
namel.text = @"10";
break;
case 10:
namel.text = @"11";
break;
case 11:
namel.text = @"12";
break;
break;
default:
break;
}
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是更简短的答案,此方法适用于控制视图的任何类(与当前操作方法相同的位置):
Here is the way shorter answer, this method goes in whatever class is controlling the view (same place as the current action method):
您可以使用
arc4random_uniform
来生成随机数。希望这能有所帮助。
You can use
arc4random_uniform
to generate the random number.Hope this can help.