使用UIAlertviewController弹出视图
2015-10-16 22:31
513 查看
iOS8中新增了UIAlertViewController,用来统一之前的UIAlertView和UIActionSheet.但是个人觉得某些情况下不太适用,因为UIAlertViewController继承自UIViewController,只能用
但是UIAlertViewController来展示UITextField的时候可以自由订制使用所有的UITextField特性了,UIAlertView只能使用系统固定的样式.
oc代码:
swift代码:
presentViewController:animated:completion方法来展示,这样的话在一些UIView的业务场景下就不好处理了.
但是UIAlertViewController来展示UITextField的时候可以自由订制使用所有的UITextField特性了,UIAlertView只能使用系统固定的样式.
oc代码:
- (IBAction)showAlert:(id)sender { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"This is title" message:@"This is message" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"OK"); }]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"Cancel"); }]; UIAlertAction *Destructive = [UIAlertAction actionWithTitle:@"Destructive" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { NSLog(@"Destructive"); }]; [alert addAction:cancel]; [alert addAction:ok]; [alert addAction:Destructive]; [self presentViewController:alert animated:YES completion:nil]; } //展示textfield - (IBAction)showTextfield:(UIButton *)sender { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"This is title" message:@"This is message" preferredStyle:UIAlertControllerStyleAlert]; [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"username"; NSLog(@"username==%@",textField.text); textField.delegate = self; }]; [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"password"; NSLog(@"password==%@",textField.text); textField.delegate = self; }]; UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"OK"); }]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"Cancel"); }]; [alert addAction:cancel]; [alert addAction:ok]; [self presentViewController:alert animated:YES completion:nil]; }
swift代码:
@IBAction func showAlert(sender: UIButton) { var alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle:.Alert) var ok = UIAlertAction(title: "ok", style: .Default) { (ok) -> Void in println("ok") } var cancel = UIAlertAction(title: "cancel", style: .Default) { (ok) -> Void in println("cancel") } alert.addAction(cancel) alert.addAction(ok) self.presentViewController(alert, animated: true) { () -> Void in println("present") } } //展示textfield @IBAction func showTextfield(sender: AnyObject) { var alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle:.Alert) alert.addTextFieldWithConfigurationHandler { (textfield) -> Void in textfield.placeholder = "Email" } var ok = UIAlertAction(title: "ok", style: .Default) { (ok) -> Void in println("ok") } alert.addAction(ok) self.presentViewController(alert, animated: true) { () -> Void in println("present") } }
相关文章推荐
- ios中关于UIImagePickerController的一些知识总结
- iOS开发——高级篇——流水布局UICollectionViewFlowLayout的基本使用
- String.valueOf() 方法的使用
- UVa 10534 Wavio Sequence
- FastGUI教程
- uImage与zImage的区别
- UIKeyBinding
- POJ——3061 Subsequence
- Unique Paths II
- Unique Paths
- POJ 2533 Longest Ordered Subsequence
- Quirks模式是什么?
- Qt的Script、Quick、QML的关系与总结
- NGUI实现技能冷却
- HDU 4010 Query on The Trees
- Scala学习笔记25【ListBuffer、ArrayBuffer、Queue、Stack常用操作实战】
- 小技巧 关于UI控件
- UI - presentViewControllerAndSingleton
- ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I
- UITextField 弹出键盘时遮盖住textField的问题