首页 > 技术文章 > UIAlertControl的使用对比与UIAlertView和UIActionSheet

li--nan 2015-04-25 12:04 原文

1.UIAlertVIew以-(void)show的方法显示:

- (void)viewDidLoad {
    [super viewDidLoad];
    //UIAlertView的使用
    [self showAlert];
    
    //UIAcyionSheet使用
//    [self showAction];
    
#pragma mark  [self showAlertController];方法不可以放在此处因为UIAlertControl继承与UIViewController
    //UIAlertControl的使用
//    [self showAlertController];
    
   }

UIAlertView 的实现:

-(void)showAlert{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"紧急通知" message:@"由于幸福指数哦不断下降,经调查和假期成线性关系,特批以后工作一天休息一天" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
 
    alertView.alertViewStyle =  UIAlertViewStyleDefault;
    /*
     UIAlertViewStyleDefault ,默认
     UIAlertViewStyleSecureTextInput, 密码输入框
     UIAlertViewStylePlainTextInput,  文本输入框
     UIAlertViewStyleLoginAndPasswordInput 用户及密码
     */
    
    [alertView show];
}

#pragma mark - UIALertDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            NSLog(@"点击%ld",buttonIndex);
            break;
            case 1:
            NSLog(@"点击%ld",buttonIndex);
            break;
        default:
            break;
    }
}

2.UIActionSheet的使用

-(void)showAction{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"请假\n选择请假类型" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"事假" otherButtonTitles:@"病假",@"产假", nil];
    [actionSheet showInView:self.view];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark UIActionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            NSLog(@"点击%ld",buttonIndex);
            break;
        case 1:
            NSLog(@"点击%ld",buttonIndex);
            break;
        case 2:
            NSLog(@"点击%ld",buttonIndex);
            break;
        default:
            break;
    }
}

3.UIAlertControl的简单使用

-(void)showAlertController{
    //UIAlertControllerStyle两种类型UIAlertControllerStyleAlert类似UIAlertView
    //UIAlertControllerStyleActionSheet类似UIActionSheet
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"you  and  me" message:@"together forever" preferredStyle:UIAlertControllerStyleAlert];
    //block代码块取代了delegate
    UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"No matter of life and death" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"生死相依");
    }];
    UIAlertAction *alertTwo = [UIAlertAction actionWithTitle:@"Share weal and woe" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"同甘共苦");
    }];
    UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Sure" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"确定");
    }];
     [alertControl addAction:actionOne];
    [alertControl addAction:alertTwo];
    [alertControl addAction:alertthree];
    
    //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
    [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.text = @"for love ";
    }];
    
    [self presentViewController:alertControl animated:YES completion:nil];
    
}

  

推荐阅读