一、UIAlerView重复弹
在开发中,经常会遇到连续的弹出提示框的情况,如果不加以处理,可能会出现重复弹出的问题。 解决方法:
// 声明全局变量
@property (nonatomic, assign) BOOL isShowingAlertView;
// 在需要弹出的地方调用该方法
- (void)showAlertView {
if (self.isShowingAlertView) {
return;
}
self.isShowingAlertView = YES;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alertView show];
}
// 在结束代理方法里,把isShowingAlertView设置为NO
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
self.isShowingAlertView = NO;
}
二、UIAlerViewController
从iOS8开始,UIAlertView已经被苹果推出的UIAlerViewController所替代。
三、UIAlerView主动弹出
在某些情况下,可能需要程序主动弹出UIAlerView,比如用户输入的信息不合法,需要给予提示。 解决方法:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
四、UIAlerView字体颜色
UIAlerView的字体颜色默认是黑色的,如果需要改变字体颜色,可以通过设置富文本来实现。 解决方法:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
NSMutableAttributedString *alertString = [[NSMutableAttributedString alloc] initWithString:@"Message"];
[alertString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 7)]; // 从第0个字符开始,长度为7个字符
[alertView setValue:alertString forKey:@"attributedMessage"];
[alertView show];
五、UIAlerViewController自定义
UIAlerViewController提供了自定义的接口,可以根据自己的需要来进行布局和样式的调整。 解决方法:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[subView setBackgroundColor:[UIColor yellowColor]];
[alertController.view addSubview:subView];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
六、UIAlerViewController苹果
苹果在UIAlerViewController中提供了一些默认的样式,包括UIAlertControllerStyleAlert和UIAlertControllerStyleActionSheet。 解决方法:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
七、UIAlerViewController竖排了
有些情况下,UIAlerViewController中的按钮可能会变成竖排的,这是由于按钮过多而导致的,可以通过设置preferredAction属性来指定主按钮,避免出现竖排情况。 解决方法:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDestructive handler:nil];
// 设置preferredAction属性
[alertController addAction:cancelAction];
[alertController addAction:destructiveAction];
[alertController addAction:okAction];
alertController.preferredAction = okAction;
[self presentViewController:alertController animated:YES completion:nil];