Swift iOS -NSMutableAttributedString 未呈现 ViewController?

Swift iOS -NSMutableAttributedString Not Presenting ViewController?

我正在向 textView 添加一个 NSMutableAttributedString,当我单击 tappableString 时,我想显示一个新的 vc。 vc 没有显示,我不知道哪里出错了。

我设置了 textView 委托并使用了 textView 的 ...shouldInteractWithURL 并在里面检查 url 的绝对字符串是否匹配 NSLinkAttributeName's 的值 [=24] =] 但 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
    }
}

我想你忘了使用故事板名称实例化视图控制器

这里"Main"是故事板名称。

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 {

}