本文介绍了根据控制台,委托似乎无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我有两节课。如何使 JournalPage 调用 JournalEntryController didSubmit 方法。

Here i have two classes. How do I make JournalPage call JournalEntryController didSubmit method.

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)
    }

这是我要调用该方法的类。

And here is the class where I want to call the method.

class JournalEntryController: UIPageViewController, UIPageViewControllerDataSource, JournalPageDelegate  {

    func didSubmit(for commentText: String) {
        print("Testing for text")
    }
}

由于某些原因,当我在 JournalPage 类。如何使 JournalPage 调用 JournalEntryController didSubmit 方法?

And for some reason, I don't see "Testing" on the console when I tap save on JournalPage class. How do I make JournalPage call JournalEntryController didSubmit method?

推荐答案

无论何时使用委托,都需要将该委托从一个视图控制器传递给另一个视图控制器。
根据Apple的定义:

Whenever you used delegates you need to pass that delegate from one view controller to another view controller.According to Apple definition:

委派是一种简单而强大的模式,其中程序中的一个对象代表或协调,另一个对象。委托对象保留对另一个对象(委托人)的引用,并在适当的时候向其发送消息。该消息通知事件的委托者委托对象将要处理或已经处理。委托可以通过更新自身或应用程序中其他对象的外观或状态来响应消息,并且在某些情况下,委托可以返回影响即将发生的事件的处理方式的值。委托的主要价值在于,它使您可以轻松自定义一个中央对象中多个对象的行为。

您要做的缺失部分是您没有为在类 JournalEntryController 中调用 JournalTextDelegate 的示例调用委托,因此您需要调用此 JournalTextDelegate 到您的 JournalPage

The missing part you are doing is that you are not calling the delegate for expample you called JournalTextDelegate in your class JournalEntryController so you need to call this JournalTextDelegate to your JournalPage.

例如:假设您要转到另一个通过push方法的视图控制器

for example: Suppose your going to another view controller through push method

let vc = self.storyboard?.instantiateViewController(withIdentifier: "identifierier") as! JournalPage
 vc.delegate = self // you need to call this delegate
 self.navigationController?.pushViewController(notifDetailVCObj, animated: true)

它将正常工作。作为参考,请参阅文档

And it will work fine. For reference see documentation https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html

这篇关于根据控制台,委托似乎无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:58