对话框

对话框 UIAlertView在8.0之后就被UIAlertController替换了

#pragma mark - 加载视图
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化两个按钮
    self.btnShow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.btnShow.frame = CGRectMake(20, 100, 100, 50);
    //设置标题
    [self.btnShow setTitle:@"start" forState:UIControlStateNormal];
    //添加开启事件方法
    [self.btnShow addTarget:self action:@selector(shows:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btnShow];
    
 }
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    //获得当前索引按钮的标题
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    NSLog(@"%@",title);
}
#pragma mark - show
-(IBAction)shows:(id)sender{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"My Title" message:@"show to you" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@"Other", nil];
    [alert show];
}

  8.0之后有代替的控件

#pragma mark - show
-(IBAction)shows:(id)sender{
//    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"My Title" message:@"show to you" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",@"Other", nil];
//    [alert show];
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil  //nil  这一项就会隐藏
                                                                   message:@"This is an alert." //nil  这一项就会隐藏
                                                            preferredStyle:UIAlertControllerStyleAlert];
//    typedef enum UIAlertControllerStyle: NSInteger {
//        UIAlertControllerStyleActionSheet = 0, //底端出现的  就是 UiActionSheet
//        UIAlertControllerStyleAlert            //对话框
//    } UIAlertControllerStyle;
    
    UIAlertAction* OKAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action) {
                                        NSLog(@"%@",action.title);
                                    }];
    UIAlertAction* CancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                               NSLog(@"%@",action.title);
                                                          }];
    //addAction  添加
    [alert addAction:OKAction];
    [alert addAction:CancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值