Xcode 中的一个 IBAction 可以有多个 UIAlertView 吗?

发布于 2024-11-29 02:10:56 字数 156 浏览 0 评论 0原文

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

Is it possible to program multiple UIAlertViews for one IBAction in Xcode to show at random. For example: I am making an app with multiple questions shown at random, when the submit button is pressed, an alert is shown saying if the answer is correct or not. I want there to be different messages for the alert such as one time it shows one message, then the next time it shows a different message at random. How would I program this?

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

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

发布评论

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

评论(2

清风无影 2024-12-06 02:10:56

在您的 .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];

In your .h:

@interface MyViewController : UIViewController { 
    NSArray *messages;
}

@property (nonatomic, retain) NSArray *messages;

In your .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];
}

When you need an alert:

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];
笨死的猪 2024-12-06 02:10:56

为项目定义以下宏:

对于 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]");

define the following macro for the project:

for the msg section as try a Array with random Index

#define KAlert(TITLE,MSG) [[[[UIAlertView alloc] initWithTitle:(TITLE) 
          message:(MSG) 
         delegate:nil 
cancelButtonTitle:@"OK" 
otherButtonTitles:nil] autorelease] show]

Which could be used as simple call:

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

or KAlert(@"Title",@"[youarray objectatindex:randindex]");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文