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

UIAlertController小示例

2015-12-17 18:49 417 查看
ios9新加的UIAlertController,感觉不错挺好用,当做个笔记

- (void)viewDidLoad {

    [super
viewDidLoad];

    UIButton * button1 = [[UIButton
alloc]initWithFrame:CGRectMake(100,
100, 100,
50)];

    [button1 setTitle:@"ActionSheet"
forState:UIControlStateNormal];

    button1.backgroundColor = [UIColor
blackColor];

    [button1 addTarget:self
action:@selector(showActionSheet)
forControlEvents:UIControlEventTouchUpInside];

    [self.view
addSubview:button1];

    

    UIButton * button2 = [[UIButton
alloc]initWithFrame:CGRectMake(100,
170, 100,
50)];

    [button2 setTitle:@"AlertView"
forState:UIControlStateNormal];

    button2.backgroundColor = [UIColor
blackColor];

    [button2 addTarget:self
action:@selector(showAlertView)
forControlEvents:UIControlEventTouchUpInside];

    [self.view
addSubview:button2];

}

#pragma mark action sheet

- (void)showActionSheet

{

    UIAlertController * alertC = [UIAlertController
alertControllerWithTitle:@"提示"
message:@"今天星期四"
preferredStyle:UIAlertControllerStyleActionSheet];

    

    UIAlertAction * defaultBtn = [UIAlertAction
actionWithTitle:@"普通"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {

        NSLog(@"点击了普通按钮");

    }];

    UIAlertAction * cancelBtn = [UIAlertAction
actionWithTitle:@"取消"
style:UIAle
4000
rtActionStyleCancel
handler:^(UIAlertAction *action) {

        NSLog(@"点击了取消按钮");

    }];

    UIAlertAction * destBtn = [UIAlertAction
actionWithTitle:@"销毁"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {

        NSLog(@"点击了销毁按钮");

    }];

    

    //添加到actionsheet

    [alertC addAction:defaultBtn];

    [alertC addAction:cancelBtn];

    [alertC addAction:destBtn];

    

    //显示到屏幕

    [self
presentViewController:alertC animated:YES
completion:nil];

}

#pragma mark alert view

- (void)showAlertView

{

    UIAlertController * alertC = [UIAlertController
alertControllerWithTitle:@"提示"
message:@"后天星期六是个好日子"
preferredStyle:UIAlertControllerStyleAlert];

    

    //添加按钮

    UIAlertAction * cancelBtn = [UIAlertAction
actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {

        NSLog(@"点击了取消按钮");

    }];

    

    UIAlertAction * destBtn = [UIAlertAction
actionWithTitle:@"重置"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {

        NSLog(@"点击了重置按钮");

        //获取输入框的内容

        UITextField * tf = alertC.textFields[0];

        NSLog(@"%@",tf.text);

        

    }];

    

   
//将按钮添加到弹框上

    [alertC addAction:cancelBtn];

    [alertC addAction:destBtn];

    

    //添加输入框

    [alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.placeholder =
@"请输入用户名";

    }];

    [alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.placeholder =
@"请输入密码";

    }];

    [alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {

       textField.placeholder =
@"请输入银行卡密码";

    }];

    //显示到屏幕上

    [self
presentViewController:alertC animated:YES
completion:nil];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  博客 xcode ios app