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

iOS UIAlertController弹窗效果

2016-07-25 15:30 489 查看
在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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息