使用UIAlertviewController弹出视图

本文介绍了iOS8引入的UIAlertViewController,它是UIAlertView和UIActionSheet的替代品,但因继承自UIViewController,仅能通过present方法展示,使得在某些UIView业务场景中应用受限。然而,对于展示UITextField,它提供了更多自定义的可能性,可以充分利用UITextField的所有特性。文中给出了Objective-C和Swift的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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")
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值