新方法:iOS9.0以后用新方法
设置中间的弹出框
UIAlertController
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置一个Button,作用是点击它,弹出警告框
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
myButton.backgroundColor = [UIColor redColor];
[myButton setTitle:@"点击" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
}
//Button的方法
-(void)haha:(id)a{
//iOS9.0之后用新方法
//新建一个弹出框alert
//后面的可以选从底部弹出
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请检查网络" preferredStyle:UIAlertControllerStyleAlert];
//新建一个输入用户名的框
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.keyboardType = UIAlertActionStyleDefault;
}];
//新建一个输入密码的框
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//设置密码格式(输入后是小点)
textField.secureTextEntry = YES;
//设置输入框弹出键盘样式(这里设置为数字键盘)
textField.keyboardType = UIKeyboardTypeNumberPad;
//设置键盘右下角return键的类型
textField.returnKeyType = UIReturnKeyYahoo;
}];
//设置取消按钮和点击后的后续操作响应事件
//在handler:处直接按键盘上的回车键,就会展开
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"用户点击了取消");
}]];
//设置确认按钮和点击后的后续操作响应事件
//!!!其实这里都一样,只是“确认”两字不一样,后面还可以加好多个(视具体情况而定,设置自己想要的功能按钮就好了)
[alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"用户点击了确认");
//比如:如果点击了确认,就打印输入的用户名和密码
NSLog(@"%@, %@", [[alert textFields]objectAtIndex:0].text
, [[alert textFields]objectAtIndex:1].text);
}]];
//将设置赋给弹出框,并且可以弹出,很重要!!!
[self presentViewController:alert animated:YES completion:nil];
}