Ios 我可以在Xcode中为一个iAction拥有多个UIAlertView吗?

Ios 我可以在Xcode中为一个iAction拥有多个UIAlertView吗?,ios,xcode,uialertview,ibaction,Ios,Xcode,Uialertview,Ibaction,可以为Xcode中的一个iAction编程多个UIAlertView以随机显示吗。例如:我正在制作一个随机显示多个问题的应用程序,当按下submit按钮时,会显示一条提示,说明答案是否正确。我希望警报有不同的消息,比如一次显示一条消息,然后下一次随机显示另一条消息。我该如何编程?在您的.h中: @interface MyViewController : UIViewController { NSArray *messages; } @property (nonatomic, reta

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

在您的.h中:

@interface MyViewController : UIViewController { 
    NSArray *messages;
}

@property (nonatomic, retain) NSArray *messages;
在你的房间里

@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];

为项目定义以下宏:

对于msg部分,请尝试使用随机索引的数组

#define KAlert(TITLE,MSG) [[[[UIAlertView alloc] initWithTitle:(TITLE) 
          message:(MSG) 
         delegate:nil 
cancelButtonTitle:@"OK" 
otherButtonTitles:nil] autorelease] show]
可以用作简单的调用:

KAlert(@"Title", @"Message"); 

or KAlert(@"Title",@"[youarray objectatindex:randindex]");

它说消息是一个未声明的标识符。。。我需要做些什么来解决这个问题?当我运行代码时,程序只针对一个问题工作,但它随后崩溃并显示错误“程序接收信号:“EXC\u BAD\u ACCESS”“我做错了什么?”?我已经复制并粘贴了你的代码,所以我知道它都是正确键入的。