Swift iOS -NSMutableAttributedString不呈现ViewController?

问题描述 投票:0回答:2

我正在向textView添加NSMutableAttributedString,当我单击tappableString时,我想要呈现一个新的vc。 vc没有出现,我无法弄清楚我哪里出错了。

我设置了textView委托,并使用textView的...shouldInteractWithURL,在那里我检查,看到url的绝对字符串与NSLinkAttributeName's值匹配,即“doSomething”,但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
    }
}
ios swift uitextview nsmutablestring
2个回答
0
投票

我想您忘记使用其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)

0
投票

问题是我使用的是textView的委托方法的旧方法签名:

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool{

}

一旦我使用自动完成并且出现了更新的方法签名,一切正常:

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {

}
© www.soinside.com 2019 - 2024. All rights reserved.