问题描述
我正在通过单击textview中的url链接来导航屏幕。
这是我的代码
extension ViewController: UITextViewDelegate {
func textView(_ textView: UITextView,shouldInteractWith URL: URL,in characterRange: NSRange,interaction: UITextItemInteraction) -> Bool {
print("click event url:",URL)
let vc = storyboard?.instantiateViewController(withIdentifier: "blogsViewController") as! BlogsViewController
vc.url = URL.absoluteString
navigationController?.pushViewController(vc,animated: true)
return true
}
}
但是此链接向野生动物园浏览器开放,而不是导航到特定控制器。
解决方法
如果要打开其他视图控制器而不是浏览器,则只能在委托方法中返回false。
extension ViewController: UITextViewDelegate {
func textView(_ textView: UITextView,shouldInteractWith URL: URL,in characterRange: NSRange,interaction: UITextItemInteraction) -> Bool {
print("click event url:",URL)
let vc = storyboard?.instantiateViewController(withIdentifier: "blogsViewController") as! BlogsViewController
vc.url = URL.absoluteString
navigationController?.pushViewController(vc,animated: true)
return false //instead of true
}
}
这应该足以满足您的目的。