iOS UIAlertController弹窗效果

在iOS8之前弹窗一共有两种方式分为UIAlertView和UIActionSheet,在iOS8之后新增UIAlertViewController来统一管理,下面就来一一作介绍

1.UIAlertView

// message 可设置为nil,cancelButtonTitle也可设置为nil,otherButtonTitles可设置多个button

 UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"温馨提示" message:@"你要选择点击哪一个" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定"]; 

 [alert show]; 

 [alert release]

 

//代理方法

 -(void)alertView:(UIAlertView *)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex

{

  NSLong(@”你点击的是第%d个”, buttonIndex);

}

2.UIActionSheet

UIActionSheet *actionSheet =[[UIActionSheet alloc] initWithTitle:@"温馨提示" delegate:self  cancelButtonTitle:@"取消"  destructiveButtonTitle:@"确定"  otherButtonTitles:@"待定"]; 

[actionSheet showInView:self.view]; 

[actionSheet release]; 

//代理方法

- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 

   NSLog(@"点击了第%d个", buttonIndex); 

   if (buttonIndex == actionSheet.cancelButtonIndex) { 

       return; 

   } 

   switch (buttonIndex) { 

       case 0: { 

           NSLog(@"你点击了确定"); 

           break; 

       } 

       case 1: { 

           NSLog(@"你点击了待定"); 

           break; 

       } 

      

   } 

 

3.UIAlertController

// preferredStyle设置弹窗样式,本文用的是UIAlertControllerStyleAlert,具体可根据实际要求选择


UIAlertController *alertcontroller =[UIAlertController alertControllerWithTitle:@"温馨提示"message:@"你已经进入警告区域"  preferredStyle:UIAlertControllerStyleAlert];


UIAlertAction*action = [UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)) {

       NSLog(@"你点击了确定");

   }];    


UIAlertAction *action1 = [UIAlertActionactionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

       NSLog(@"你点击了取消");

   }];

  

   [alertcontroller addAction:action];

   [alertcontroller addAction:action1];

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章