gpt4 book ai didi

swift - 如何修复 Method does not override any method from its superclass error with UITextViewDelegate on Swift?

转载 作者:行者123 更新时间:2023-11-28 13:21:19 30 4
gpt4 key购买 nike

我正在尝试覆盖 textView shouldInteractWithURL 方法来处理可点击链接。所以我创建了一个继承自 UITextView 和 UITextViewDelegate 的子类,调用 MyTextView 并等待使用此代码覆盖该方法:

import UIKit

class MyTextView: UITextView, UITextViewDelegate {

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

println(URL)
return false
}
}

但我收到一条错误消息:方法没有覆盖其父类(super class)中的任何方法。

如何解决?谢谢。

最佳答案

您不应该将 UITextView 子类化。您应该实现并添加委托(delegate)。

class MyViewController : UIViewController, UITextViewDelegate {
override func viewDidLoad() {
var textView = UITextView(frame: CGRectMake(0, 0, view.frame.width, view.frame.height))
textView.delegate = self
view.addSubview(textView)
}

// MARK: UITextViewDelegate
func textView(textView: UITextView!, shouldInteractWithURL URL: NSURL!, inRange characterRange: NSRange) -> Bool {
println(URL)
return false
}
}

关于swift - 如何修复 Method does not override any method from its superclass error with UITextViewDelegate on Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25419983/

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