我可以用什么开始代码来创建一个简单的UIAlertView,上面只有一个"OK“按钮?
发布于 2010-12-17 01:56:13
当您希望显示警报时,请执行以下操作:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ROFL"
message:@"Dee dee doo doo."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
// If you're not using ARC, you will need to release the alert view.
// [alert release];
如果要在单击按钮时执行某些操作,请实现此委托方法:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// the user clicked OK
if (buttonIndex == 0) {
// do something here...
}
}
并确保您的代理符合UIAlertViewDelegate
协议:
@interface YourViewController : UIViewController <UIAlertViewDelegate>
发布于 2015-11-21 06:49:57
iOS 8不推荐使用UIAlertView。因此,要在iOS 8及更高版本上创建警报,建议使用UIAlertController:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
// Enter code here
}];
[alert addAction:defaultAction];
// Present action where needed
[self presentViewController:alert animated:YES completion:nil];
这就是我实现它的方式。
发布于 2010-12-17 01:55:51
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:nil //or self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert autorelease];
https://stackoverflow.com/questions/4463806
复制相似问题