警告框_前进的火车_新浪博客


一、老的警告框

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title"

  message:@"message" delegate:nil cancelButtonTitle:@"NO" 

  otherButtonTitles:@"YES",nil]; 

2.设置警告框的代理对象,当用户对alertView有任何操作时,都会交给代理对象来进行响应

alert.delegate = self;

3.设置警告框的显示样式(共4种)

alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

4.显示警告框

[alert show]; 

5.成为委托方代理需遵循协议<</span>UIAlertViewDelegate>,协议中方法有:

- (void)alertView:(UIAlertView *)alertView

     clickeonAtIndex:(NSInteger)buttonIndex; //点击按钮携带文本框信息

- (void)alertViewCancel:(UIAlertView *)alertView;//点击cancel按钮

- (void)willPresentAlertView:(UIAlertView *)alertView;//警告框即将展现

- (void)didPresentAlertView:(UIAlertView *)alertView;//警告框已经展现

- (void)alertView:(UIAlertView *)alertView

     willDismissWithButtonIndex:(NSInteger)buttonIndex;//文本框即将消失

- (void)alertView:(UIAlertView *)alertView

     didDismissWithButtonIndex:(NSInteger)buttonIndex;//文本框已经消失 

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView*)alertView;


二新版警告框(支持ios8以上系统,目前iphone4最高能升到7.1系统)



1.创建alertController实例,并说明样式

UIAlertController *alertController = [UIAlertController   

   alertControllerWithTitle:@"title" message:@"message" 

   preferredStyle:UIAlertControllerStyleAlert];

2.创建按钮行为

UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES" 

   style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

        UITextField *nameText = alertController.textFields[0]; //获取在文本框中输入的文字

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

    }];

    UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"NO"style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

            NSLog(@"点击了NO按钮");

    }];

3.添加行为到控制器中

    [alertController addAction:yesAction];

    [alertController addAction:noAction];

4.添加文本框到控制器中

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        //对添加的文本框进行设置

        textField.font = [UIFont systemFontOfSize:24];

        textField.textColor = [UIColor redColor];

}];

5.推出控制器,显示界面

[self presentViewController:alertController animated:YES completion:nil]; //两个控制器之间的切换,方法一就是使用present方法

}

6.UIAlertControllerStyle类型:

   1、 UIAlertControllerStyleActionSheet = 0

   2、 UIAlertControllerStyleAlert

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值