gpt4 book ai didi

ios - Swift iOS -NSMutableAttributedString 未呈现 ViewController?

转载 作者:行者123 更新时间:2023-11-30 11:55:39 24 4
gpt4 key购买 nike

我正在将 NSMutableAttributedString 添加到 textView 中,当我单击 tappableString 时,我想呈现一个新的 vc。 VC 没有被呈现,我不知道哪里出了问题。

我设置了textView委托(delegate)并使用textView的...shouldInteractWithURL,并在里面检查url的绝对字符串是否与NSLinkAttributeName的值匹配,即“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
}
}

最佳答案

我认为您忘记使用 Storyboard 名称实例化 View Controller

这里的“Main”是 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)

关于ios - Swift iOS -NSMutableAttributedString 未呈现 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47854066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com