缺少使用导航控制器从第一视图控制器到第二视图控制器的过渡

发布于 2025-01-21 23:37:02 字数 1078 浏览 1 评论 0原文

我想创建一个导航层次结构,我想在其中使用NavigationController从FirstViewController转到SecondViewController。 FirstViewController包含一个我打算使用的按钮B。我正在使用导航控制器概念,因为我想稍后返回第一视图controller。我已经完成了以下步骤来实现它:

  1. 我将导航式controller嵌入了一个故事板X中,其中conteber controller和secondViewController既有。
  2. 我已经从第一视图中的按钮B创建了一个segue(已与之关联的FirstViewController)到第二视图(具有SecondViewController)。
  3. 我将NavigationViewController NVC设置为以下内容,如我提出了FirstViewController:
nvc = UINavigationController.init(rootViewController: firstViewController)
  1. 稍后,当按钮B被按下时,我执行以下代码将SecondViewController推到导航层次结构:
self.nvc?.pushViewController(secondViewController, animated: true)

但是,即使在按下秒数也不显示自身,即使在按住后, 。

  1. 我将按钮B和SecondViewController之间的SEGUE命名为Ksegue,因此我试图执行SEGUE作为补充,以查看SecondViewController是否表现出来:
self.performSegue(withIdentifier: "kSegue", sender: self)

当第4和第5个步骤一起执行时,就会出现例外。例外指出,我正在推动已经存在于导航层次结构上的ViewController,但是即使我对Performsegue代码进行了评论,第二视图仍然没有打开。

我可以问我在这里犯的错误吗?

I want to create a navigation hierarchy where I want to go to a SecondViewController from FirstViewController using a NavigationController. The FirstViewController contains a button B that I intend to use to go to a SecondViewController. I am employing the navigation controller concept since I want to return to the FirstViewController later. I have done the following steps to achieve it:

  1. I have embedded a NavigationController into a storyboard X containing both the FirstViewController and SecondViewController.
  2. I have created a segue from the button B present in the FirstView (has FirstViewController associated with it) to the SecondView (has SecondViewController).
  3. I set the navigationViewController nvc as following after I have presented the firstViewController:
nvc = UINavigationController.init(rootViewController: firstViewController)
  1. Later on, when the button B gets pressed, I execute the following code to push the secondViewController onto the navigation hierarchy:
self.nvc?.pushViewController(secondViewController, animated: true)

However, the secondView doesn't present itself even after pushing.

  1. I have named the segue between the button B and secondViewController as kSegue, so I tried to perform the segue as an addition to see if the secondViewController presents itself or not:
self.performSegue(withIdentifier: "kSegue", sender: self)

An exception occurs when both the 4th and 5th steps are performed together. The exception states that I'm pushing a viewController that already exists on the navigation hierarchy, but the second view still doesn't open even if I comment the performSegue code.

May I ask what mistake I am making here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水微香 2025-01-28 23:37:02

在故事板中,请确保从导航控制器到第一个视图控制器有一个rootviewController segue。另外,请确保将导航控制器标记为初始视图控制器。

在代码中,更改

self.nvc?.pushViewController...

self.navigationController?.pushViewController...

In the storyboard, make sure there is a rootViewController segue from the navigation controller to the first view controller. Also, make sure the navigation controller is marked as the initial view controller.

In the code, change

self.nvc?.pushViewController...

To

self.navigationController?.pushViewController...
天暗了我发光 2025-01-28 23:37:02
1) Take a navigation controller on your storyboard
2) Set navigation controller as initial view controller 
3) set view controller A as a root view controller of your navigation controller

4) in "GoToB" method access View controller B's instance and push it in navigation controller 

5) On View controller B's "Go Back" method write code to pop it.

6) Dont forget to set storyboard Id on both A & B view controller

class FirstViewController: UIViewController
    {
        
                lazy var secondViewController : SecondViewController? =
                {
                    let secondViewController =  UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
                    return secondViewController
                }()
                
                override func viewDidLoad()
                {
                    super.viewDidLoad()
                }
                
                @IBAction func goToB(sender : UIButton)
                {
                    guard let secondViewController = self.secondViewController else
                    {
                        return
                    }
                    
                    self.navigationController?.pushViewController(secondViewController, animated: true)
                }
            }
    
    
    
    class SecondViewController: UIViewController 
    {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
        
        @IBAction func goBack(sender : UIButton)
        {
            self.navigationController?.popViewController(animated: true)
        }
    }
1) Take a navigation controller on your storyboard
2) Set navigation controller as initial view controller 
3) set view controller A as a root view controller of your navigation controller

4) in "GoToB" method access View controller B's instance and push it in navigation controller 

5) On View controller B's "Go Back" method write code to pop it.

6) Dont forget to set storyboard Id on both A & B view controller

enter image description here

class FirstViewController: UIViewController
    {
        
                lazy var secondViewController : SecondViewController? =
                {
                    let secondViewController =  UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
                    return secondViewController
                }()
                
                override func viewDidLoad()
                {
                    super.viewDidLoad()
                }
                
                @IBAction func goToB(sender : UIButton)
                {
                    guard let secondViewController = self.secondViewController else
                    {
                        return
                    }
                    
                    self.navigationController?.pushViewController(secondViewController, animated: true)
                }
            }
    
    
    
    class SecondViewController: UIViewController 
    {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
        
        @IBAction func goBack(sender : UIButton)
        {
            self.navigationController?.popViewController(animated: true)
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文