uialertController的使用方法

通过添加输入框使用uialertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@“提示” message:@“请输入Section名称” preferredStyle:UIAlertControllerStyleAlert];
//以下方法就可以实现在提示框中输入文本;

//在AlertView中添加一个输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    
    textField.placeholder = @"姓名";
}];

//添加一个确定按钮 并获取AlertView中的第一个输入框 将其文本赋值给BUTTON的title
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    UITextField *envirnmentNameTextField = alertController.textFields.firstObject;
    [self.dataSource addObject:envirnmentNameTextField.text];
    //输出 检查是否正确无误
    NSLog(@"你输入的文本%@",envirnmentNameTextField.text);
    
}]];

//添加一个取消按钮
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];

//present出AlertView
[self presentViewController:alertController animated:true completion:nil];

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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来跳转到指定的网页。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值