Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何向警报添加url?_Swift_Nsurl_Uialertcontroller - Fatal编程技术网

Swift 如何向警报添加url?

Swift 如何向警报添加url?,swift,nsurl,uialertcontroller,Swift,Nsurl,Uialertcontroller,是否有一种方法可以为寻找其他信息的用户添加url?我的警报是这样的 let alert = UIAlertController(title: "Alert details", message: "For more detailed information, click the link below", preferredStyle: UIAlertControllerStyle.Alert) // add url here let okayAction = UIAlertActi

是否有一种方法可以为寻找其他信息的用户添加url?我的警报是这样的

let alert = UIAlertController(title: "Alert details", message: "For more detailed information, click the link below", preferredStyle: UIAlertControllerStyle.Alert)
    // add url here
    let okayAction = UIAlertAction(title: "Ok", style: .Default) { (action) in
        print(action)
    }
    alert.addAction(okayAction)

其想法是将它们重定向到网页并关闭应用程序中的UIAlertController

您不能向UIAlertController添加任意接口。标题和消息不可点击。您不能再添加文本。相反,添加另一个按钮(UIAlertAction),将它们指向网页。或者使用其他接口而不是UIAlertController(例如,您可以设置一个看起来像警报的显示视图控制器)


从这里开始:

谢谢。是否也可以使用'alert.addTextFieldWithConfigurationHandler{(textField)}->Void in'创建与URL关联的字符串??我不确定文本字段将如何帮助您。文本字段中可能没有足够的空间显示整个URL,用户无法在其中执行任何可能触发某种操作的操作。触发操作的是按钮。您当然可以将自己指定为文本字段的委托人,但我不会我不太明白这对你有什么帮助。谢谢马特,我在文本字段上玩了一点,但你是对的,处理这个问题的唯一方法是使用UIAlertControlleri上的按钮。如果你想制作自己的自定义警报,我有一个项目可以帮你开始:看起来有点黑……如果你要这么做的话更多的定制可能最好只创建自己的警报视图。
let alert = UIAlertController(title: "Default Title", message: nil, preferredStyle: .alert)
let textView = UITextView()
textView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

let controller = UIViewController()

textView.frame = controller.view.frame
controller.view.addSubview(textView)
textView.backgroundColor = .clear

alert.setValue(controller, forKey: "contentViewController")

let height: NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 150)
alert.view.addConstraint(height)

let attributedString = NSMutableAttributedString(string: "http://movies.com")
let url = URL(string: "http://movies.com")

attributedString.setAttributes([.link: url ?? ""], range: NSMakeRange(0, attributedString.length))


textView.attributedText = attributedString
textView.isUserInteractionEnabled = true
textView.isEditable = false

// Set how links should appear: blue and underlined
textView.linkTextAttributes = [
    .foregroundColor: UIColor.blue,
    .underlineStyle: NSUnderlineStyle.single.rawValue
]

let okAction = UIAlertAction(title: "OK", style: .default) {
    UIAlertAction in
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {
    UIAlertAction in
}

alert.addAction(okAction)
alert.addAction(cancelAction)

present(alert, animated: true, completion: nil)