UIAlertController基本应用

iOS8.0之后,苹果公司推出了最新的UIAlertController用来代替UIActionSheet  和UIAlertView.

---UIActionSheet---

UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:@"保存或删除数据"message:@"删除数据将不可恢复"preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*action) {

NSLog(@"取消!!!");//点击方法转移到了这里

}];

UIAlertAction*deleteAction = [UIAlertActionactionWithTitle:@"删除"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction*action) {

NSLog(@"删除!!!");

}];

UIAlertAction*archiveAction = [UIAlertActionactionWithTitle:@"保存"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action) {

NSLog(@"保存!!!");

}];

[alertControlleraddAction:cancelAction];

[alertControlleraddAction:deleteAction];

[alertControlleraddAction:archiveAction];

[viewController presentViewController:alertController animated:YES completion:nil];

---UIAlertView---

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"A Short Title Is Best"message:@"A message should be a short, complete sentence."preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAlter = [UIAlertAction actionWithTitle:@"Cancel"style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");

}];

UIAlertAction *otherAlter = [UIAlertAction actionWithTitle:@"OK"style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

NSLog(@"The \"Okay/Cancel\" alert's other action occured.");

}];

// Add the actions.

[alert addAction:cancelAlter];

[alert addAction:otherAlter];

[viewController presentViewController:alert animated:YES completion:nil];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容