相对于IOS8.4之后苹果对提示框做了进一步的封装,这将与之前的提示框有很大的同。
之前的 UIAlterView 是弹出一个提示框。
而今天学习的提示框是 通过视图控制器进行弹出,这就意味着,我们可以在这个提示框上添加更多的处理事件,我认为苹果的之所以这样是希望用户能够将提示框的功能发挥的淋漓尽致,效果会更加的炫酷。
这里仅仅将基础知识写出来,以供查阅与巩固。
UIAlertController * alterCon = [UIAlertController alertControllerWithTitle:@”提示” message:@”不需要” preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction * alterTion = [UIAlertAction actionWithTitle:@”警告” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alterCon addAction:alterTion];
[self presentViewController:alterCon animated:NO completion:nil];
实际上提示的内容是:alterCon 里面定义的提示内容。
在alterCon 里 有的一个属性值(就是上面写的) :UIAlertControllerStyleActionSheet 是让提示框从底部出现。
还有一个属性值:UIAlertControllerStyleAlert 是让提示框从中部出现。
资料参考
有的时候,我们希望实现如下的效果:
上面的代码实现的效果的关键源代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIAlertController * alterCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"你好,你可以这样做更好,请【确定】;你好,你可以这样做更好,请【取消】;你好,你可以这样做更好,请【稍后在说】" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction * alterTion1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"选择了这个——确定");
}];
UIAlertAction * alterTion2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"选择了这个——取消");
}];
UIAlertAction * alterTion3 = [UIAlertAction actionWithTitle:@"稍后在说" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"选择了这个——稍后再说");
}];
[alterCon addAction:alterTion1];
[alterCon addAction:alterTion2];
[alterCon addAction:alterTion3];
[self presentViewController:alterCon animated:NO completion:nil];
}
代码
此外也可以实现下面的效果:
实现效果代码为:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"添加好友" preferredStyle:UIAlertControllerStyleAlert]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { }]; UITextField *textField = alert.textFields[]; UIAlertAction *queding = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 发起好友的添加请求 }];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:queding];
[alert addAction:cancel]; // 显示警示框
[self presentViewController:alert animated:YES completion:nil];
代码