UIiOSiOS Developer

关于UIAlertController的用法

2015-12-31  本文已影响1961人  4ba6804ff45f

关于UIAlertController的用法

简介

在新版本的Xcode中增加了UIAlertController而UIAlertView在调用时则会显示已经过期。本人在使用中经常会使用到Alert窗口,所以整理一下用法以方便自己使用。

基本语法

一、基础控件的添加(原UIAlertView)

UIAlertController *alertOne = [UIAlertController alertControllerWithTitle:@"I'm alertOne" message:@"I want to tell you something" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertOne animated:YES completion:nil];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

[alertOne addAction:cancel];

UIAlertAction *certain = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];

[alertOne addAction:certain];
[alertOne addTextFieldWithConfigurationHandler:
^(UITextField * _Nonnull textField) {

  // 监听

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];

  // 定制键盘和输入框背景文字及clearButton

  textField.placeholder = @"请输入";

  textField.clearButtonMode = UITextFieldViewModeWhileEditing;

}];


- (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification

{

UITextField *textField = notification.object;

self.textLabel.text = textField.text;

}

二、从下方弹出式UIAlertController(原UIActionSheet)


NSString *title = NSLocalizedString(@"AlertTwo", nil);

UIAlertController *alertTwo = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];


UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

[alertTwo addAction:cancel];

UIAlertAction *certain = [UIAlertAction actionWithTitle:@"清空" style:UIAlertActionStyleDestructive handler:
^(UIAlertAction * _Nonnull action) {

   self.textLabel.text = nil;

}];

[alertTwo addAction:certain];

需要源代码点我

未完待续!

上一篇下一篇

猜你喜欢

热点阅读