警告视图及操作表单在xcode7.0中的使用

警告视图(alert)及操作表单(action sheet)都用于向用户提供反馈。(模态视图)

操作表单:要求用户在两个以上选项之间做出选择。操作表单从屏幕底部出现,显示一系列按钮供用户选择。用户必须点击其中一个按钮之后才能继续使用应用程序。操作表单通常用于向用户确认有潜在危险的或者无法撤销的操作,比如删除对象。

警告视图:以圆角矩形的形式出现在屏幕中央。与操作表单类似,警告视图也要求用户必须作出一个回应,然后才能继续使用应用程序。警告视图通常用于通知用户发生了一些重要的或者不寻常的事情。与操作表单不同,警告视图可以只显示一个按钮,但是如果需要接受多个回应的话,也允许显示多个。

使用方式如下代码:

    NSString *title = @"A short Title is Best";
    NSString *message = @"A message should be a short, complete sentence";
    NSString *cancelButtonTitle = @"Cancel";
    NSString *otherButtonTitle = @"OK";
    
    //警告框
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    //添加输入框(警告框才具备)
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        // 可以对textField进行定制,如修改背景色
        textField.backgroundColor = [UIColor orangeColor];
    }];
    
//    //操作表单
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    
    //create the actions
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Cancel action occured.");
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Other action occured.");
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    //显示
    [self presentViewController:alertController animated:YES completion:nil];

 

posted @ 2016-04-06 10:22  smile_smile  阅读(362)  评论(0编辑  收藏  举报