一、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];
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/238238.html