【发布时间】:2020-10-10 09:51:51
【问题描述】:
当控件/状态位于 secondViewController 中时,如何在另一个 firstViewController swift 文件中调用函数/方法。 在 SecondViewController 中,当按下按钮时,secondViewController 应调用 firstViewController 中的函数并将控制/状态转移到从 secondViewController 推送到的 thirdViewController。
secondViewController Button Action
@IBAction func EnterGallery(_ sender: Any){
// Want to invoke EnterGallery function in firstViewController and dismiss from secondViewController
self.dismiss(animated: true, completion: nil)}
firstViewController pushViewController function
func EnterGallery() {
let dest = self.storyboard?.instantiateViewController(withIdentifier:
"GalleryViewController") as! GalleryViewController // thirdViewController
self.navigationController?.pushViewController(dest, animated: true)
}
请注意:我没有将任何数据从 secondViewController 传递到 firstViewController。我只想让我的 firstViewController 推送到 thirdViewController,而我只是从 secondViewController 中解散,后者是从 firstViewController 以当前功能呈现的。 一旦我从 secondViewController 中解散,我希望我的屏幕直接转到 thirdViewController。
基本上我只想在另一个 ViewController 中调用一个函数,而不需要从初始 ViewController 传递任何数据。所以我不能使用协议和代表或通知和观察者。我应该如何处理这个? 在许多其他情况下,我需要使用这种类似的功能。所以我不确定如何准确执行此操作。 由于我是 Swift 新手,任何帮助将不胜感激。 提前致谢。
【问题讨论】:
-
您可以为此使用闭包...在我看来最适合此类任务
标签: ios swift xcode uiviewcontroller swift4.2