【问题标题】:Xcode: How to know the viewController that has presented the current view?Xcode:如何知道呈现当前视图的viewController?
【发布时间】:2020-12-23 15:26:49
【问题描述】:

我有两个视图控制器“FirstViewController”和“SecondViewControler”,其中包含一个按钮,该按钮显示一个名为“BibliothequesViewController”的新视图控制器按钮被点击:

@IBAction func onLibrariesButtonClicked(_ sender: Any) {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController
        self.present(vc, animated: true, completion: nil)
    }

目前,BibliothequesViewController 包含一个通过 Segue 重定向到 FirstViewController 的按钮:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
           print("SegWay being performed")
           // Every segway has a beginning point and ending point
           // We're trying to access the ending point
           // as type FullScreenMovieController
           // Then we're setting the chosenMovie variable of FullScreenMovieController to 1
           let vc = segue.destination as! FirstViewController
           vc.receivedSelectedLibraryFromBibliothequesList = self.selectedLibrary
           print("vc.receivedSelectedLibraryFromBibliothequesList :",  vc.receivedSelectedLibraryFromBibliothequesList )
                }

这是执行 Segue 的代码:

  self.performSegue(withIdentifier: "BiblioToLoginSegue", sender: self)

我需要实现的是:
重定向到实际呈现 BibliothequesViewController 的视图(FirstViewControllerSecondViewControler)。 上面的代码会自动重定向到 FirstViewController,无论它是否是呈现 BibliothequesViewController 的那个。

我看不出我应该采用哪种方法来实现这一点。 我应该使用推送而不是出现在 FirstViewController&SecondViewControler 中吗?

【问题讨论】:

标签: ios swift xcode uiviewcontroller tvos


【解决方案1】:

教一个类知道某事的最简单方法是添加一个属性:

class BibliothequesViewController {
    ...
    var presentedBy: UIViewController?
    ...
}

在控制器实例化后分配属性

let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController

vc.presentedBy = self

并在时机成熟时使用此属性。

【讨论】:

  • presentedBy 应该包含 FirstViewController 或 SecondViewController,这样 BibliothequesViewController 就会知道是哪个提交了它。所以你的回答一开始是对的,但我认为第二部分是错误的。
  • 我听取了您的建议并尝试了这个:stackoverflow.com/questions/63742725/… 如果可以,请检查一下并告诉我您的想法
  • 我刚刚根据您的提示实施了一个解决方案。谢谢你。可以的话就看看吧。
【解决方案2】:

我接受了@Roman 的建议,但我改变了一些事情。
警告:我不确定这是正确的方法,但它对我有用。

BibliothequesViewController

class BibliothequesViewController: UIViewController {
    
    static let sharedInstance = BibliothequesViewController()
    var presentedBy: UIViewController?
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if BibliothequesViewController.sharedInstance.presentedBy! is LoginViewController {
            let vc = segue.destination as! LoginViewController
            vc.receivedSelectedLibraryFromBibliothequesList = self.selectedLibrary
        } else {
            let vc = segue.destination as! ForgetPasswordViewController
            vc.receivedSelectedLibraryFromBibliothequesList = self.selectedLibrary
        }
    }
    
    @IBAction func onLibrarySelected(_ sender: Any) {
        if BibliothequesViewController.sharedInstance.presentedBy! is LoginViewController {
            self.performSegue(withIdentifier: "BiblioToLoginSegue", sender: self)
        } else {
            self.performSegue(withIdentifier: "biblioToPasswordForgottenSigue", sender: self)
        }
    }
    
}

登录视图控制器

class LoginViewController: UIViewController {
    @IBAction func onLibrariesButtonClicked(_ sender: Any) {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController
        BibliothequesViewController.sharedInstance.presentedBy=self
        self.present(vc, animated: true, completion: nil)
    }
}

忘记密码视图控制器

  class ForgetPasswordViewController: UIViewController {
        @IBAction func onLibrariesButtonClicked(_ sender: Any) {
            let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let vc: BibliothequesViewController = storyboard.instantiateViewController(withIdentifier: "BibliothequesViewController") as! BibliothequesViewController
            BibliothequesViewController.sharedInstance.presentedBy=self
            self.present(vc, animated: true, completion: nil)
        }
    }

注意:您必须创建两个从 BibliothequesViewControllerLoginViewControllerForgetPasswordViewController 的 Segue,并使用中提到的标识符BibliothequesViewController

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-23
    • 2012-11-29
    • 2018-06-24
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多