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

UIalertController

2015-07-14 13:20 537 查看
#import "ViewController.h"

@interface ViewController ()
@property (weak,nonatomic)
IBOutletUIButton *button1;
@property (weak,nonatomic)
IBOutletUIButton *button2;
- (IBAction)button1:(id)sender;
- (IBAction)button2:(id)sender;
@property (nonatomic,strong) UIAlertController *alertController;
@end

@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
}

- (IBAction)button1:(id)sender {
//以前创建alert的方法
// UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"标题" message:@"这个是UIalertView的默认样式"
delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
// [alertView show];
//
//1、初始化
_alertController = [UIAlertControlleralertControllerWithTitle:@"标题"
message:@"信息" preferredStyle:UIAlertControllerStyleAlert];
//1、比较两种创建的方式:初始化
//前:1)指定代理 2)初始化的过程中指定按钮 3、样式已经确定为UIAlertView
//后:1)不用指定代理 2)先不用指定按钮(这样分工就明显了) 3、需要指定样式(alert和sheet)

//2、设置按钮 1)设置按钮的title、style
、handler(也就是处理),按钮分类:(1)取消按钮(只能够有一个)(2)警示按钮:颜色为红色 (3)一般按钮
// UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel handler:^(UIAlertAction *
__nonnull action) {
// NSLog(@"");
NSLog(@"%@",action);
}];

//当视图控制器释放的时候,我们需要移除这个Observer,我们通过每一个按钮的handler代码块(还有其他任何可能释放的视图控制器的地方)中添加代码块来实现它。

UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"好的"
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
UITextField *login =_alertController.textFields.firstObject;
UITextField *password =_alertController.textFields.lastObject;


NSString* loginText = login.text;
NSString* passwordText = password.text;

NSLog(@"loginText is : %@",loginText);
NSLog(@"passwordText is : %@",passwordText);

[[NSNotificationCenterdefaultCenter]
removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:nil];
}];

// 由上面这个方法,我们可以知道,UIAlertController是按钮和方法是直接绑定的,没有相应的代理方法,我们需要设置观察者来实现相应的方法。

[_alertController addAction:cancelAction];
//添加相应的按钮对象
[_alertControlleraddAction:okAction];

okAction.enabled =NO;//设置这个“好的按钮不响应”,这样我们就通过观察者机制来实现激活“好的”

//设置警示按钮,颜色为红色
// UIAlertAction *reSetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:nil];
// [alertController addAction:reSetAction];

//3、添加UItextField

[_alertControlleraddTextFieldWithConfigurationHandler:^(UITextField *__nonnull
textField) {
textField.placeholder =@"登陆,需要3个字符才能启动好的";
//增加通知机制
//在self当前的对象中监听textField对象,放在在self中的alerttextfieldDidChange方法,
//UITextFieldTextDidChangeNotification是一个通知的标志
[[NSNotificationCenterdefaultCenter]
addObserver:self selector:@selector(alerttextfieldDidChange:) name:@"UITextFieldTextDidChangeNotification"
object:textField];
}];
[_alertControlleraddTextFieldWithConfigurationHandler:^(UITextField *__nonnull
textField) {
textField.placeholder =@"密码";
textField.secureTextEntry =YES;
}];
/*
UIAlertController 中没有代理的方法,如果我们要实现UIalertView中的delegate的方法,我们因可更改用观察者——通知机制。
eg:如果我们要让“登陆”文本框至少3个字符才能够激活“好的”按钮。
*/


[selfpresentViewController:_alertController animated:YES
completion:^{
NSLog(@"执行完成");
}];

}
#pragma mark NSNotification

- (void)alerttextfieldDidChange:(UITextField*)textfield
{
//这个方法有问题
// UIAlertController *alertController = self.presentationController;
// if (alertController) {
// UITextField *login = alertController.textFields.firstObject;
// UIAlertAction *okAction = alertController.actions.lastObject;
// okAction.enabled = login.text.length >2;
// }



//方法一:直接设置全局属性
UITextField *login =_alertController.textFields.firstObject;
UIAlertAction *okAction =_alertController.actions.lastObject;
okAction.enabled = login.text.length >2;

}

/*
UIAlertController 极大的灵活性意味着您不必拘泥于内置的样式,
旧版本:只能在默认视图、文本框视图、密码视图、登陆、密码输入框视图中选择
现在:可以任意的添加对话框中添加任意数目的UITextfield对象,并且可以使用所有的UITextField特性,
当向对话框控制器中添加文本框时,您需要指定一个用来配置文本框的代码块。

*/

//下拉菜单
- (IBAction)button2:(id)sender
{
UIButton *sender1 = (UIButton*)sender;

UIAlertController * alertController = [UIAlertControlleralertControllerWithTitle:@"title"message:@"message"preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction * cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];
UIAlertAction *deleteAction = [UIAlertActionactionWithTitle:@"删除"style:UIAlertActionStyleDestructivehandler:nil];
UIAlertAction *archiveAciton = [UIAlertActionactionWithTitle:@"保存"style:UIAlertActionStyleDefaulthandler:nil];

[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[alertController addAction:archiveAciton];

[selfpresentViewController:alertControlleranimated:YEScompletion:nil];

// UIPopoverController 过时

UIPopoverPresentationController *popover = alertController.popoverPresentationController;

if(popover ){
popover.sourceView = sender1;
popover.sourceRect = sender1.frame;
popover.permittedArrowDirections =UIPopoverArrowDirectionDown;

}
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: