【问题标题】:Not able to push which is set to root vc无法推送设置为root vc的
【发布时间】:2020-12-02 12:07:44
【问题描述】:

我有一个视图控制器(没有嵌入到导航控制器中)。假设 mainVC,从 maninVC 我有一个名为 goNext 的按钮。当我点击时,我需要对我的同一个 VC mainVC 进行一些标签更改。

所以我做到了:

这是在应用委托中:

 let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
 let exampleVC = storyBoard.instantiateViewController(withIdentifier: "MainVC")
 self.window = UIWindow(frame: UIScreen.main.bounds)
 self.window?.rootViewController = exampleVC
 self.window?.makeKeyAndVisible()

我的VC:

   func pushToMainVc() {
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let loginPageView = mainStoryboard.instantiateViewController(withIdentifier: "MainVC") as! MainVC
        let rootViewController = self.window?.rootViewController as? UINavigationController
        rootViewController?.pushViewController(loginPageView, animated: true)
    }

但它没有推动。任何帮助都会很棒。

谢谢

【问题讨论】:

    标签: ios swift xcode uinavigationcontroller


    【解决方案1】:

    您不能将相同的ViewController 推送到NavigationController

    【讨论】:

      【解决方案2】:

      为什么要再次推送/加载 mainVC? mainVC 已经加载。如果您希望在按下 goNext 按钮时发生一些标签更改,请将此标签的 IBOutlet 获取到您的 ViewController 代码中。并在代码中获取 goNext 按钮的 IBAction,当按下此按钮时更改标签。例如:

      class mainVC: UIViewController {
          ...
          @IBOutlet weak var someLabel: UILabel!
          override func viewDidLoad() {
             super.viewDidLoad()
             someLabel.text = "TextOnLoad"
          }
          ... 
          @IBAction func goNextButtonPressed(_ sender: UIButton) {
             someLabel.text = "ChangedText"
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-29
        • 1970-01-01
        • 1970-01-01
        • 2019-09-21
        • 1970-01-01
        • 2011-09-22
        • 2023-03-16
        • 2021-12-20
        相关资源
        最近更新 更多