UIAlertController
时间: 2024-04-28 17:22:38 浏览: 13
UIAlertController 是 iOS 中用于显示提示框和操作表的控件。它可以用来展示警告、确认或输入等不同类型的弹窗。
下面是一个使用 UIAlertController 显示一个简单警告框的示例:
```swift
let alertController = UIAlertController(title: "提示", message: "这是一个警告框", preferredStyle: .alert)
let okAction = UIAlertAction(title: "确定", style: .default) { (_) in
// 点击确定按钮后执行的操作
}
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
```
你可以根据需要设置标题、消息和按钮的样式和响应操作。同时,UIAlertController 还支持展示操作表和输入框等更复杂的弹窗。
这只是一个简单的示例,更复杂的用法和配置可根据具体需求进行调整。
相关问题
使用OC语言开发一个UIAlertController弹窗,要求内容包含超链接,可点击跳转Web页面
UIAlertController弹窗一般用于提示用户某些信息或进行一些简单的操作。但是,它并不支持在内容中包含超链接。因此,我们需要自定义UIAlertController弹窗,并在其中添加一个可点击的UILabel来实现超链接跳转。
以下是示例代码:
```
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题"
message:@"这是一个带超链接的提示框"
preferredStyle:UIAlertControllerStyleAlert];
// 创建富文本字符串,并添加超链接
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"点击这里跳转到百度"];
[attributedStr addAttribute:NSLinkAttributeName value:@"http://www.baidu.com" range:NSMakeRange(0, attributedStr.length)];
// 创建一个可点击的UILabel
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
label.attributedText = attributedStr;
label.textAlignment = NSTextAlignmentCenter;
label.userInteractionEnabled = YES;
[label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLabel:)]];
// 将UILabel添加到UIAlertController的view中
[alertController.view addSubview:label];
// 创建并添加UIAlertAction
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了确定按钮");
}];
[alertController addAction:okAction];
// 显示UIAlertController
[self presentViewController:alertController animated:YES completion:nil];
```
在以上代码中,我们创建了一个UIAlertController,并在其中添加了一个UILabel,将UILabel的attributedText设置为带有超链接的富文本字符串,然后添加了一个手势识别器,当用户点击UILabel时,会调用tapLabel方法,在该方法中使用UIApplication打开URL来跳转到指定的网页。最后,我们添加了一个UIAlertAction,将UIAlertController显示出来。
以下是tapLabel方法的示例实现:
```
- (void)tapLabel:(UITapGestureRecognizer *)gestureRecognizer {
UILabel *label = (UILabel *)gestureRecognizer.view;
NSRange range = [label.attributedText.string rangeOfString:@"点击这里跳转到百度"];
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
if (range.location != NSNotFound) {
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}
}
}
```
在tapLabel方法中,我们首先获取被点击的UILabel,然后查找UILabel的attributedText中是否包含指定的文本,如果包含,则使用UIApplication打开URL来跳转到指定的网页。
自定义AlertView的方法和改变Alert的弹出位置以及其宽度
自定义UIAlertView的方法已经在iOS13之后被废弃,推荐使用UIAlertController来代替。下面介绍如何自定义UIAlertController的弹出位置以及宽度。
1. 自定义弹出位置
可以使用UIAlertController的popoverPresentationController属性来设置弹出位置。具体步骤如下:
```
// 创建UIAlertController对象
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
// 创建弹出位置的UIView对象
UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
// 设置弹出位置
alertController.popoverPresentationController.sourceView = popoverView;
alertController.popoverPresentationController.sourceRect = popoverView.bounds;
// 弹出UIAlertController
[self presentViewController:alertController animated:YES completion:nil];
```
2. 自定义宽度
可以通过设置UIAlertController的preferredContentSize属性来改变其宽度。具体步骤如下:
```
// 创建UIAlertController对象
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
// 设置宽度
alertController.preferredContentSize = CGSizeMake(280, 200);
// 弹出UIAlertController
[self presentViewController:alertController animated:YES completion:nil];
```
需要注意的是,此方法只适用于UIAlertControllerStyleAlert风格的UIAlertController。如果使用UIAlertControllerStyleActionSheet风格的UIAlertController,设置preferredContentSize属性将不会生效。