VC 未连接的代表和协议

Delegates and protocols with VC not connected

我有一个父视图控制器,即 HomeViewController,它有一个导航栏按钮,用户可以通过该按钮触发警报并输入字符串。此字符串需要传递给子视图控制器。

父视图控制器中的相关代码如下:

protocol NewSectionDelegate {
    func sendSectionName(name : String)
}

class HomeViewController: UIViewController {
var sectionNameDelegate : NewSectionDelegate?

func addCardAsChild() { // add the child VC to the parent VC
if cardViewController == nil {
                cardViewController = CardViewController()
                addViewController(newViewController: cardViewController!)
            } else {
                addViewController(newViewController: cardViewController!)
            }
}

func triggerAlert() {
let alertController = UIAlertController(title: "New section", message: "Name the section with a words or a sentence", preferredStyle: .alert)
                alertController.addTextField(configurationHandler:
                    {(_ textField: UITextField) -> Void in //txtview customization
                    })

                let addAction = UIAlertAction(title: "Add", style: .default) { _ in
                    guard let sectionName = alertController.textFields?.first?.text else { return }
                    self.sectionNameDelegate?.sendSectionName(name: sectionName) // Sending string; verified that the string is not nil
                }

                alertController.addAction(addAction)
                self.present(alertController, animated: true, completion: nil)
}

这是子视图控制器:

class CardViewController: UIViewController,  NewSectionDelegate {

override func viewDidLoad() {
        super.viewDidLoad()
        let homeViewController = HomeViewController()
        homeViewController.sectionNameDelegate = self
     }

    func sendSectionName(name: String) {
print("received name:\(name)") // This line of code is never called
}

数据没有通过,我不知道为什么。

我认为正在调用委托方法,但对于您在 ChildViewController viewDidLoad 方法中创建的内部 HomeViewController。看起来您希望该方法在不同的对象上被调用。我将从 viewDidLoad 中删除该代码并在 HomeViewController

中设置 sectionNameDelegate

这是您要找的吗?

protocol NewSectionDelegate {
    func sendSectionName(name : String)
}

class HomeViewController: UIViewController {
    var sectionNameDelegate : NewSectionDelegate?
    var cardViewController = CardViewController()

    func addCardAsChild() { // add the child VC to the parent VC
        self.addChild(self.cardViewController)
    }

    func triggerAlert() {
        let alertController = UIAlertController(title: "New section", message: "Name the section with a words or a sentence", preferredStyle: .alert)
        alertController.addTextField(configurationHandler:
            {(_ textField: UITextField) -> Void in //txtview customization
        })

        let addAction = UIAlertAction(title: "Add", style: .default) { _ in
            guard let sectionName = alertController.textFields?.first?.text else { return }
            self.sectionNameDelegate?.sendSectionName(name: sectionName) // Sending string; verified that the string is not nil
        }

        alertController.addAction(addAction)
        self.present(alertController, animated: true, completion: nil)
    }
}

class CardViewController: UIViewController,  NewSectionDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        guard let homeViewController = self.parent as? HomeViewController else { return }
        homeViewController.sectionNameDelegate = self
    }

    func sendSectionName(name: String) {
        print("received name:\(name)") // This line of code is never called
    }
}