OGeek|极客世界-中国程序员成长平台

标题: ios - 我可以在 Xcode 中为一个 IBAction 设置多个 UIAlertViews 吗? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 08:48
标题: ios - 我可以在 Xcode 中为一个 IBAction 设置多个 UIAlertViews 吗?

是否可以在 Xcode 中为一个 IBAction 编写多个 UIAlertViews 以随机显示。例如:我正在制作一个随机显示多个​​问题的应用程序,当按下提交按钮时,会显示一条警报,说明答案是否正确。我希望警报有不同的消息,例如一次它显示一条消息,然后下一次它随机显示一条不同的消息。我该如何编程?



Best Answer-推荐答案


在您的 .h 中:

@interface MyViewController : UIViewController { 
    NSArray *messages;
}

@property (nonatomic, retain) NSArray *messages;

在你的 .m 中

@implementation MyViewController
@synthesize messages;

- (dealloc) {
    [messages release];
}

- (void)viewDidLoad {
    messages = [[NSArray alloc] initWithObjects"Funny Message", @"Even Funnier Message", @"Hilarious message", @"ROFL", @"OK this is getting boring...", nil];
}

当您需要警报时:

NSUInteger messageCount = [messages count];
int randomMessageIndex = arc4random() % messageCount;

UIAlertView *alert = [[UIAlertView alloc] initWithTitle"Title" message:[messages objectAtIndex:randomMessageIndex] delegate:nil cancelButtonTitle"OK" otherButtonTitles:nil];
[alert show];
[alert release];

关于ios - 我可以在 Xcode 中为一个 IBAction 设置多个 UIAlertViews 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7033839/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4