未为 UITextView 触发的委托协议函数
Delegate protocol functions not triggered for UITextView
我有这个 swift 文件,我试图在其中创建自定义 class 来处理一页上多个文本视图的占位符。我更喜欢单独的 class,因为我认为它可以更清晰地处理多个视图。问题是,当这些事件发生时,函数 textViewDidEndEditing 和 textViewDidBeginEditing 没有被调用。
我已经通过将 PostPage 设置为委托成功地工作了,但是我不知道如何使用多个 UITextView 来做到这一点。
这是我的代码:
import UIKit
class PostPage: UIViewController, UITextViewDelegate {
@IBOutlet weak var eventDescription: UITextView!
@IBOutlet weak var eventTitle: UITextView!
var placeholderShowing = true
override func viewDidLoad() {
super.viewDidLoad()
// Add a placeholder for the text
let ph_descr = PlaceHolderHandler(placeholderText: "Description", textBox: eventDescription)
eventDescription.delegate = ph_descr
let ph_title = PlaceHolderHandler(placeholderText: "Title", textBox: eventTitle)
eventTitle.delegate = ph_title
}
}
class PlaceHolderHandler: NSObject, UITextViewDelegate {
var placeholderShowing = true
var placeholderText = ""
required init(placeholderText: String, textBox: UITextView) {
super.init()
self.placeholderText = placeholderText
textBox.text = placeholderText
textBox.textColor = UIColor.lightGray
}
// MARK:- UITextViewDelegates
public func textViewDidBeginEditing(_ textView: UITextView) {
if self.placeholderShowing {
textView.text = ""
textView.textColor = UIColor.black
self.placeholderShowing = false
}
}
public func textViewDidEndEditing(_ textView: UITextView) {
if textView.text == "" {
textView.text = "Description"
textView.textColor = UIColor.lightGray
self.placeholderShowing = true
}
}
}
当我 select 任一 UITextView 时,我希望调用相应的函数,并相应地调整文本。
您需要保留 phDescr
和 phTitle
,因为在您的代码中它们是局部变量,因此在 viewDidLoad
的末尾取消分配
import UIKit
class PostPage: UIViewController, UITextViewDelegate {
@IBOutlet weak var eventDescription: UITextView!
@IBOutlet weak var eventTitle: UITextView!
var placeholderShowing = true
var phDescr, phTitle:PlaceHolderHandler!
override func viewDidLoad() {
super.viewDidLoad()
// Add a placeholder for the text
phDescr = PlaceHolderHandler(placeholderText: "Description", textBox: eventDescription)
eventDescription.delegate = phDescr
phTitle = PlaceHolderHandler(placeholderText: "Title", textBox: eventTitle)
eventTitle.delegate = phTitle
}
}
也使用驼峰式命名变量
I've had it successfully work by setting the PostPage to the delegate, but I don't see how I could do that with multiple UITextView
当然你可以将两者的委托设置为 PostPage
并知道哪一个是完整的使用
func textViewDidBeginEditing(_ textView: UITextView) {
if textView == eventDescription {
}
else { // eventTitle
}
}
我有这个 swift 文件,我试图在其中创建自定义 class 来处理一页上多个文本视图的占位符。我更喜欢单独的 class,因为我认为它可以更清晰地处理多个视图。问题是,当这些事件发生时,函数 textViewDidEndEditing 和 textViewDidBeginEditing 没有被调用。
我已经通过将 PostPage 设置为委托成功地工作了,但是我不知道如何使用多个 UITextView 来做到这一点。
这是我的代码:
import UIKit
class PostPage: UIViewController, UITextViewDelegate {
@IBOutlet weak var eventDescription: UITextView!
@IBOutlet weak var eventTitle: UITextView!
var placeholderShowing = true
override func viewDidLoad() {
super.viewDidLoad()
// Add a placeholder for the text
let ph_descr = PlaceHolderHandler(placeholderText: "Description", textBox: eventDescription)
eventDescription.delegate = ph_descr
let ph_title = PlaceHolderHandler(placeholderText: "Title", textBox: eventTitle)
eventTitle.delegate = ph_title
}
}
class PlaceHolderHandler: NSObject, UITextViewDelegate {
var placeholderShowing = true
var placeholderText = ""
required init(placeholderText: String, textBox: UITextView) {
super.init()
self.placeholderText = placeholderText
textBox.text = placeholderText
textBox.textColor = UIColor.lightGray
}
// MARK:- UITextViewDelegates
public func textViewDidBeginEditing(_ textView: UITextView) {
if self.placeholderShowing {
textView.text = ""
textView.textColor = UIColor.black
self.placeholderShowing = false
}
}
public func textViewDidEndEditing(_ textView: UITextView) {
if textView.text == "" {
textView.text = "Description"
textView.textColor = UIColor.lightGray
self.placeholderShowing = true
}
}
}
当我 select 任一 UITextView 时,我希望调用相应的函数,并相应地调整文本。
您需要保留 phDescr
和 phTitle
,因为在您的代码中它们是局部变量,因此在 viewDidLoad
import UIKit
class PostPage: UIViewController, UITextViewDelegate {
@IBOutlet weak var eventDescription: UITextView!
@IBOutlet weak var eventTitle: UITextView!
var placeholderShowing = true
var phDescr, phTitle:PlaceHolderHandler!
override func viewDidLoad() {
super.viewDidLoad()
// Add a placeholder for the text
phDescr = PlaceHolderHandler(placeholderText: "Description", textBox: eventDescription)
eventDescription.delegate = phDescr
phTitle = PlaceHolderHandler(placeholderText: "Title", textBox: eventTitle)
eventTitle.delegate = phTitle
}
}
也使用驼峰式命名变量
I've had it successfully work by setting the PostPage to the delegate, but I don't see how I could do that with multiple UITextView
当然你可以将两者的委托设置为 PostPage
并知道哪一个是完整的使用
func textViewDidBeginEditing(_ textView: UITextView) {
if textView == eventDescription {
}
else { // eventTitle
}
}