简体   繁体   English

如何从TabbarController选择视图控制器?

[英]How to segue to view controller from TabbarController?

I have a TabbarController with is the entry point of my app.So I want to segue for this TabbarController to ViewControllerA . 我有一个TabbarController ,它是我应用程序的入口点,所以我想将此TabbarControllerViewControllerA I perform performSegue(with: "identifier") .I can reach the ViewControllerA ,but when I reach ViewControllerA there is no NavigationBar 我执行performSegue(with: "identifier") 。我可以到达ViewControllerA ,但是当我到达ViewControllerA ,没有NavigationBar

I tried to embed ViewControllerA into a NavigationController 1st,so the storyboard look like this 我试图将ViewControllerA嵌入到NavigationController 1st中,所以故事板看起来像这样

TabbarController-> NavigationController -> ViewControllerA TabbarController-> NavigationController-> ViewControllerA

By this,I can reach ViewControllerA.But I have problem in prepareForSegue method.Cause I need to include a value in the segue,now the value seem not included. 这样,我可以到达ViewControllerA。但是我在prepareForSegue方法中遇到了问题。因为我需要在segue中包含一个值,现在似乎不包含该值。

My code in prepareForSegue is look like this : 我在prepareForSegue中的代码如下所示:

if let VcA = segue.destination.navigationController?.topViewController as? ViewControllerA {
  VcA.postId = sender as! Int
}else{
   print("cant work")
}

And I also tried to embed the tabbarController to a NavigationController,but in Xcode-> Editor -> Embed the option is being disable. 我也尝试将tabbarController嵌入到NavigationController中,但是在Xcode-> Editor -> Embed该选项被禁用。

So my question is 所以我的问题是

how to segue from a TabbarController to ViewController and in destination the NavigationBar is visible and can go back to previous screen? 如何从TabbarController切换到ViewController,并且在目标位置的NavigationBar是可见的,并且可以返回到上一屏幕?

Assuming that your storyboards looks similar to: 假设您的情节提要类似于:

在此处输入图片说明

That's because you are calling performSegue(with: "identifier") in the TabbarController which does not directly connected to the segue. 那是因为您在TabbarController中调用了performSegue(with: "identifier") ,而TabbarController 没有直接连接到segue。 What you should do instead so to implement it in the first view controller which connected to the navigation controller ( ViewControllerA ). 您应该怎么做才能在连接到导航控制器( ViewControllerA )的第一个视图控制器中实现它。 So, it would be -for instance- something like this: 因此,例如:

class ViewControllerA: UIViewController {
    // ...

    override func viewDidLoad() {
        super.viewDidLoad()

        performSegue(with: "identifier")

    }

    // ...
}

Thus your segue should be from ViewControllerA to the desired view controller. 因此,您的选择应该是从ViewControllerA到所需的视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM