iOS 学习笔记 九 (2015.04.02)IOS8中使用UIAlertController创建警告窗口

时间:2023-11-09 22:52:50

1、IOS8中使用UIAlertController创建警告窗口

#pragma mark - 只能在IOS8中使用的,警告窗口
- (void)showOkayCancelAlert
{
    NSString *title = NSLocalizedString(@"修改组名", nil);
    NSString *message = NSLocalizedString(@"请输入新的组名", nil);
    NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);
    NSString *otherButtonTitle = NSLocalizedString(@"确定", nil);
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
     {
         textField.backgroundColor = [UIColor lightTextColor];       // 可以在这里对textfield进行定制,例如改变背景色
     }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"%s------%d-----点击取消按钮", __FUNCTION__, __LINE__);
    }];
    [alertController addAction:cancelAction];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UITextField *textField = alertController.textFields[0];                                                                 // 获取输入框中的新的组名
        if ( textField.text.length == 0 )
        {
            [[[UIAlertView alloc] initWithTitle:@"提示" message:@"您的新群组名不合理" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            return;
        }
        NSLog(@"%s------%d-----点击确定按钮-----textField.text = %@", __FUNCTION__, __LINE__, textField.text);
        [[SingleClient sharedInstanceClient] ClientAlterGroupInfor:(int)self.curGroup.g_02_gid gname:textField.text];           // 向服务器发送修改组名的请求
    }];
    [alertController addAction:otherAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}