zar*_*don 6 iphone objective-c uialertview
我厌倦了编写基本的UIAlertView,即:
UIAlertView *alert = [[UIAlertView alloc] initWith...]] //etc
Run Code Online (Sandbox Code Playgroud)
是不是可以将所有这些放在一个"帮助器"函数中,我可以返回buttonIndex,或者通常返回什么警报?
对于一个简单的辅助函数,我猜你可以为标题,消息提供参数,我不确定你是否可以在参数中传递委托,或者捆绑信息.
在伪代码中,它可能是这样的:
someValueOrObject = Print_Alert(Title="", Message="", Delegate="", Bundle="") // etc
Run Code Online (Sandbox Code Playgroud)
对此的任何帮助都会很棒.
谢谢
zou*_*oul 14
在4.0+中,您可以使用块简化警报代码,有点像这样:
CCAlertView *alert = [[CCAlertView alloc]
initWithTitle:@"Test Alert"
message:@"See if the thing works."];
[alert addButtonWithTitle:@"Foo" block:^{ NSLog(@"Foo"); }];
[alert addButtonWithTitle:@"Bar" block:^{ NSLog(@"Bar"); }];
[alert addButtonWithTitle:@"Cancel" block:NULL];
[alert show];
Run Code Online (Sandbox Code Playgroud)
当我厌倦了做同样的事情时,这是我写的:
-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName {
[self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: nil];
}
-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName informing:(id)delegate {
[self alert: title withBody: message firstButtonNamed: firstButtonName withExtraButtons: nil informing: delegate];
}
-(void)alert:(NSString *)title withBody:(NSString *)message firstButtonNamed:(NSString *)firstButtonName withExtraButtons:(NSArray *)otherButtonTitles informing:(id)delegate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: title
message: message
delegate: delegate
cancelButtonTitle: firstButtonName
otherButtonTitles: nil];
if (otherButtonTitles != nil) {
for (int i = 0; i < [otherButtonTitles count]; i++) {
[alert addButtonWithTitle: (NSString *)[otherButtonTitles objectAtIndex: i]];
}
}
[alert show];
[alert release];
}
Run Code Online (Sandbox Code Playgroud)
不过,您无法编写一个函数来显示警报,然后返回一个像 ButtonIndex 这样的值,因为只有当用户按下按钮并且您的委托执行某些操作时才会发生该值返回。
换句话说,用 提出问题的过程UIAlertView是一个异步过程。