【发布时间】:2018-09-14 07:53:52
【问题描述】:
这里我有两节课。如何使JournalPage 调用JournalEntryController didSubmit 方法。
protocol JournalPageDelegate {
func didSubmit(for commentText: String)
}
class JournalPage: UIViewController, UITextViewDelegate {
var delegate: JournalPageDelegate?
fileprivate let textView: UITextView = {
let textView = UITextView()
let attributedText = NSMutableAttributedString(string: "Enter Text Here.", attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 18)])
textView.textColor = UIColor.black
textView.backgroundColor = UIColor.white
textView.becomeFirstResponder()
textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
textView.attributedText = attributedText
return textView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(save))
}
@objc func save() {
print("saving")
guard let commentText = textView.text else { return }
delegate?.didSubmit(for: commentText)
}
这是我要调用该方法的类。
class JournalEntryController: UIPageViewController, UIPageViewControllerDataSource, JournalPageDelegate {
func didSubmit(for commentText: String) {
print("Testing for text")
}
}
由于某种原因,当我在 JournalPage 类上点击保存时,我没有在控制台上看到“正在测试”。如何使JournalPage 调用JournalEntryController didSubmit 方法?
【问题讨论】:
-
您的
JournalEntryController是如何将自己设置为代表的? -
在 JournalEntryController 中将委托设置为 self。
-
我想,你需要在你想调用的类中添加委托
-
您还需要使委托变弱,并使协议成为类协议。否则会出现内存泄漏。
标签: ios swift delegates uipageviewcontroller