一、iOS8介绍
iOS8 新特性,主要是UI上进行了统一
1、UIAlertController
2、UIPresentaionController:管理所有通过modal出来的控制器(看笔记)
3、UIPopoverPresentationController
4、SizeClass + Autolayout
5、App Extension 应用扩展
6、Core Image(iOS 5开始就有了,滤镜美图秀秀型 )
二、UIAlertController
1、以往我们使用的时候,都是用的 UIAlertView和UIActionSheet
//选项弹出等
-(void)alertViewOld
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;//4种方式
[alert show]; //如果要监听点击事件则需要去设置对应的代理,并在代理方法中操作
} //底部弹出
-(void)aletSheetOld
{
UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:@"" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"犹豫" otherButtonTitles:@"确定", nil]; [action showInView:self.view];
}
2、而从iOS8开始, UIAlertController = UIAlertView + UIAlertSheet
#pragma mark -- new iOS8 -(void)new
{
//1、创建中间的弹出:UIAlertView
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"弹出信息" preferredStyle:UIAlertControllerStyleAlert]; //创建底部的弹出:UIAlertSheet
//UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"弹出信息" preferredStyle:UIAlertControllerStyleActionSheet]; //2、添加按钮(不需要代理了) //这里需要防止循环引用(可以通过新建一个自己的控制器继承UIAlertController,在dealloc中打印信息,验证是否有最终释放)
__weak typeof(alert) weakAlert = alert;
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { NSLog(@"%@",[weakAlert.textFields.firstObject text]); }]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"默认" style:UIAlertActionStyleDefault handler:nil]]; //3、添加文本框(只能是添加到UIAlertControllerStyleAlert的样式,如果是preferredStyle:UIAlertControllerStyleActionSheet的话会崩溃的)
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.textColor =[UIColor redColor];
textField.text = @""; //监听文字的改变(如果是用通知的话,移除的时机需要注意,点击确定或者取消的时候就需要去移除,比较麻烦)
[textField addTarget:self action:@selector(textFieldsValueDidChange:) forControlEvents:UIControlEventEditingChanged];
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.secureTextEntry = YES;
textField.text = @"";
}]; //4、弹出控制器,要想是不是用modal
[self presentViewController:alert animated:YES completion:nil];
} -(void)textFieldsValueDidChange:(UITextField *)textField
{
NSLog(@"%@",textField.text);
}
3、在iPhone和iPad中同时适用的方式
#pragma mark -- new iOS8 in iPad
/**
* 注意:
UIAlertControllerStyleActionSheet
1、不能有文本框
2、在iPad中,必须使用popover的形式展示 以后都是用present
*/
-(void)newInpad
{
//1、创建底部的弹出:UIAlertSheet
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"弹出信息" preferredStyle:UIAlertControllerStyleActionSheet]; // 1.1 这句代码可写可不写,因为默认就会以UIModalPresentationPopover的形式存在
alert.modalPresentationStyle = UIModalPresentationPopover; // 1.2 设置popover指向按钮(这句代码iPhone中会被忽略,iPad中才会实现)
alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem; //2、添加按钮(不需要代理了) //这里需要防止循环引用(可以通过新建一个自己的控制器继承UIAlertController,在dealloc中打印信息,验证是否有最终释放)
__weak typeof(alert) weakAlert = alert;
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { NSLog(@"%@",[weakAlert.textFields.firstObject text]); }]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"默认" style:UIAlertActionStyleDefault handler:nil]]; //4、弹出控制器,要想是不是用modal
[self presentViewController:alert animated:YES completion:nil];
}
注意:
UIAlertControllerStyleActionSheet
1、不能有文本框
2、在iPad中,必须使用popover的形式展示