从另一个控制器访问它时,UILabel(作为出口)保持为零(弹出 window)

UILabel (as an outlet) remains nil when accessing it from another controller (popup window)

有 2 个控制器。 this is how the UI looks this is how I am creating the popup i.e. using segue

主视图控制器: 有一个标签和一个按钮。单击此名为 --> 'add action' --> 的按钮后,将打开一个弹出窗口。

弹出视图控制器: 是一个弹出窗口,用户可以在其中输入输入并点击保存。在弹出窗口中,有一个文本字段。用户填写该字段并单击保存。 保存后 --> 为此 PopupViewController 调用了一个 IBAction,它具有 dismiss() 函数来关闭弹出窗口。 并且 dismiss 函数内的完成块是我尝试实例化 HomeViewController 以更新标签的地方。 (注意:PopupViewController 是通过 segue 创建的,通过选择当前上下文)

但是在这样做的时候,我看到 HomeViewController 中的所有 IBOutlets 在尝试从弹出窗口中保存时都是 nil,因此我无法更新标签。

我尝试过的: 我在网上查看了其他问答,并在下面进行了验证: 1. 我检查了标签与故事板的连接。这是正确的。 2. 我使用了 instantiateViewController 语法来实例化 HomeViewController 正确。 但问题仍然存在。

HomeViewController

var actionNames = [String]()
var actionDescriptions = [String]()
@IBOutlet weak var firstActionLabel: UILabel!

func updateViewWithNewAction() {
    print("updating views started")
    if firstActionLabel != nil {
        if actionNames.count > 0 {
            firstActionLabel.text = actionNames[0]
        } else {
            print("no actions in the array")
        }
    } else {
        print("firstActionLabel is nil")
    }
    print("updating views completed")
}

func addActions(actionName: String, actionDescription: String) {
    actionNames.append(actionName)
    actionDescriptions.append(actionDescription)
    print("actions added")
}

PopupViewController

@IBOutlet weak var actionTitle: UITextField!
@IBOutlet weak var actionDescriptionTextView: UITextView!
@IBAction func createAction(_ sender: Any) {
    //getting action values from user
    actionName = actionTitle.text!
    actionDescription = actionDescriptionTextView.text!

    dismiss(animated: true, completion: {
        print("completion block started ---")
        let homeVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController

        homeVC.addActions(actionName: self.actionName, actionDescription: self.actionDescription)
        homeVC.updateViewWithNewAction()
        print("completion block ended ---")
    })
}

预期结果:在 PopupViewController 的 createAction() 之后,HomeViewController 内的标签应更新为新值。 实际结果:firstActionLabel 为零,在 homeVC.updateViewWithNewAction()

期间

这样做

class HomeViewController: UIViewcontroller {

    func viewDidLoad() {
        super.viewDidLoad()
        updateViewWithNewAction() 
    }

问题是,在 viewDidLoad 之前,你们中的任何一个 IBOutlet 都不会被初始化。他们应该通过情节提要进行初始化。在 viewDidLoad.

之前您无法获得任何参考

如果不想每次都调用updateViewWithNewAction(),那就加一个Bool标志来决定调用还是不调用updateViewWithNewAction()。需要时从 PopUpViewController 或任何其他地方设置 Bool 标志。

基本上我认为你想要这样的东西:

加入HomeViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let popupViewController = segue.destination as? PopupViewController {
        popupViewController.callback = self.addActions
    }
}

并在PopupViewController中添加

var callback: ((String, String) -> Void)?

然后在PopupViewController

@IBAction func createAction(_ sender: Any) {
    //getting action values from user
    actionName = actionTitle.text!
    actionDescription = actionDescriptionTextView.text!

    dismiss(animated: true, completion: {
        callback?(self.actionName, self.actionDescription)
    })
}