-1

我有一个视图控制器,它呈现另一个视图控制器,并且在呈现的视图控制器中,用户做出选择,这将导致许多其他通知被推送,但是当我们关闭演示视图控制器时,我希望原始父视图控制器得到通知,因为 viewwillappear 没有触发.

4

2 回答 2

1

您可以将块处理程序添加到您的子控制器,以通知父控制器用户选择:

struct Choice {
  // whatever object that represents the user choice
}

class ChildController: UIViewController {

  var completionHandler: ((ChildController, Choice) -> Void)?

  func finishPresentation(with choice: Choice) {
     // Suppose this function is called when user picks something in the user interface
     completionHandler?(self, choice)
  }

}

然后在父控制器中,分配completionHandler以获取用户选择的通知。

class ParentController: UIViewController {
  func presentChild() {
     let controller = ChildController()

     controller.completionHandler = { child, choice
        child.dismiss(animated: true) {
          // do something with the user choice
        }
     }

     present(controller, animated: true)
  }
}
于 2021-09-17T10:20:34.500 回答
1

您可以在孩子即将被解雇时发布通知,并在父视图控制器中观察通知。

在父视图控制器中

NotificationCenter.default.addObserver(self, selector: #selector(methodtobecalled(_:)), name: "childdismissed", object: nil)

 @objc func methodtobecalled(_: Notification) {
        
    }

在子视图控制器中,当您选择关闭时,发送通知

NotificationCenter.default.post(name: "childdismissed", object: nil, userInfo: nil)

采用UIAdaptivePresentationControllerDelegate呈现的视图控制器中的类

添加下面的委托方法

func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
        return false
    }

func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
    dismiss(animated: true, completion: nil)
   // Post the notification here

}

于 2021-09-17T17:08:04.527 回答