iOS开篇——UI之UAlertView(提示框)

创建提示框

 //创建提示框
    //标题  提示内容  代理对象  按钮
    
    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"萨达姆已经做好战斗准备" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"不确定", nil];

 设置提示框样式

    alertView .alertViewStyle = UIAlertViewStylePlainTextInput;
    /*
     typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
     UIAlertViewStyleDefault = 0,
     UIAlertViewStyleSecureTextInput,   密文输入
     UIAlertViewStylePlainTextInput,    明文输入
     UIAlertViewStyleLoginAndPasswordInput  明文+密文
     } __TVOS_PROHIBITED;
     */

让alertView显示出来

[alertView show];

设置代理 实现协议方法

alertView.delegate = self;
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        NSLog(@"点击了取消");
        NSLog(@"%@",[alertView textFieldAtIndex:0].text);
    }else{
        NSLog(@"点击了确定");
    }
}

iOS9之后不建议使用 

UIAlertController+UIAlertControllerStyleAlert 也可以实现同样功能

原文地址:https://www.cnblogs.com/gwkiOS/p/4990242.html