是否有可能爲Xcode中的一個IBAction編程多個UIAlertView以隨機顯示。例如:我正在製作一個隨機顯示多個問題的應用程序,當按下提交按鈕時,會顯示一條提示,說明答案是否正確。我希望在那裏爲警報提供不同的消息,例如一次顯示一條消息,然後下一次隨機顯示不同的消息。我將如何編程?我可以在Xcode中爲一個IBAction使用多個UIAlertViews嗎?
2
A
回答
2
在你的.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];
1
定義項目的下面的宏:
爲msg部分a試試隨機指數一個陣列
#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]");
相關問題
- 1. 我可以在一個腳本中使用多個函數嗎?
- 2. 我可以在一個類中使用多個ActionListeners嗎?
- 3. 我可以在一個MTLRenderCommandEncoder中使用多個MTLRenderPipelineState嗎?
- 4. 我可以在一個表單中使用多個DataGridViewRows嗎?
- 5. 我可以在一個Android drawable中使用多個形狀嗎?
- 6. 我可以在一個Drawable XML中使用多個漸變嗎?
- 7. 我可以爲多個向量使用一個內存池嗎?
- 8. 我可以爲多個@ app.route使用一個WTForm嗎?
- 9. 我可以在grails中爲同一個字段使用多個ilike嗎?
- 10. 我可以在Sass中爲一個屬性使用多個值嗎?
- 11. 我可以在一個頁面上使用多個Schema.org itemscope嗎?
- 12. 我可以在一個LineSeries上使用多個ItemRenderer嗎?
- 13. Ruby:我可以在一個語句中多次使用「或」(||)嗎?
- 14. 我可以在xcode中打開多個調試控制檯嗎?
- 15. 我可以在xcode中同時運行多個目標嗎?
- 16. 我可以使用多個foreach嗎?
- 17. 我可以使用多個「with」嗎?
- 18. 我可以不使用多個NavDropdown嗎?
- 19. 我可以使用Xcode編寫一個Excel加載項嗎?
- 20. PyPy可以在一個進程中使用多個CPU嗎?
- 21. 你可以在一個triggerAction中使用多個意圖嗎? [LUIS]
- 22. 可以在同一個分類器中使用多個ngram嗎?
- 23. 我可以在xcode中編程創建一個.xls文件嗎?
- 24. 你可以爲一個變量使用多個elifs嗎?
- 25. 我可以在一個Android活動中使用兩個onNewIntent()嗎?
- 26. 我可以在一個Silverlight項目中使用2個App.XAML嗎?
- 27. 我可以在一個變量中有多個值嗎?
- 28. 我可以在多個Mac上從App Store安裝Xcode 4嗎?
- 29. 我可以使用一個uchars數組作爲單個uchar嗎?
- 30. 我可以使用NPAPI在一個DLL中創建多個插件嗎?
它說的消息是一個未聲明的標識符...什麼我需要做的,解決這個問題? –
在您的.h中聲明消息 - 請參閱更新後的問題 –
當我運行代碼時,該程序適用於一個問題,但它會崩潰並顯示錯誤「Program received signal:」EXC_BAD_ACCESS「」我在做什麼錯?我有複製並粘貼你的代碼,所以我知道它的輸入都是正確的。 –