您的位置:首页 > 产品设计 > UI/UE

使用UIAlertviewController弹出视图

2015-10-16 22:31 513 查看
iOS8中新增了UIAlertViewController,用来统一之前的UIAlertView和UIActionSheet.但是个人觉得某些情况下不太适用,因为UIAlertViewController继承自UIViewController,只能用

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")
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: