![](/img/trans.png)
[英]presenting ViewController with NavigationViewController swift
[英]Swift iOS -NSMutableAttributedString Not Presenting ViewController?
我将NSMutableAttributedString添加到textView中,当我单击tappableString时,我想显示一个新的vc。 没有显示vc,我无法弄清楚哪里出了问题。
我设置了...shouldInteractWithURL
委托,并使用了...shouldInteractWithURL
的...shouldInteractWithURL
,在其中检查URL的绝对字符串是否与NSLinkAttributeName's
值“ doSomething”匹配, NSLinkAttributeName's
显示vc。
我哪里出问题了?
class FirstController: UIViewController, UITextViewDelegate {
fileprivate let textView: UITextView = {
let textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
textView.isEditable = false
textView.isSelectable = true
textView.textAlignment = .center
textView.isScrollEnabled = true
return textView
}()
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
configureTextView()
}
override func viewDidLayoutSubviews() {
textView.setContentOffset(.zero, animated: false)
}
func configureTextView(){
//textView anchors are set
let firstPlainSent = "Read this first.\n"
let secondPlainSent = "Read this second.\n"
plainSentAttr = [NSFontAttributeName: UIFont(name: "Helvetica", size: 17)!, NSForegroundColorAttributeName: UIColor.black]
let firstSent = NSAttributedString(string: firstPlainSent, attributes: plainSentAttr)
let secondSent = NSAttributedString(string: secondPlainSent, attributes: plainSentAttr)
let mutableAttributedString = NSMutableAttributedString()
mutableAttributedString.append(firstSent)
mutableAttributedString.append(secondSent)
let tappableString = NSMutableAttributedString(string: "Click here to present the SecondVC\n\n")
tappableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica", size: 17)!, range: NSMakeRange(0, (tappableString.length)))
tappableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSMakeRange(0, (tappableString.length)))
tappableString.addAttribute(NSUnderlineStyleAttributeName, value: 1, range: NSMakeRange(0,tappableString.length))
tappableString.addAttribute(NSUnderlineColorAttributeName, value: UIColor.blue, range: NSMakeRange(0, tappableString.length))
tappableString.addAttribute(NSLinkAttributeName, value: "doSomething", range: NSMakeRange(0, (tappableString.length)))
mutableAttributedString.append(tappableString)
textView.attributedText = mutableAttributedString
}
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
// I also tried URL.scheme
if URL.absoluteString == "doSomething"{
let secondVC = SecondController()
let navVC = UINavigationController(rootViewController: secondVC)
present(navVC, animated: true, completion: nil)
return false
}
return true
}
}
我认为您忘了使用其Storyboard名称实例化视图控制器
这里的“主要”是情节提要名称。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondController") as! SecondController
所以最终的代码就像
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondController") as! SecondController
let navVC = UINavigationController(rootViewController: secondVC)
present(navVC, animated: true, completion: nil)
问题是我正在使用textView的委托方法的旧方法签名:
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool{
}
一旦我使用了自动完成功能,并且出现了较新的方法签名,一切都将正常工作:
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.