简体   繁体   English

迅速,选择从标签栏控制器中拆分视图控制器

[英]Swift, segue to split View Controller from Tab Bar Controller

I would like to preface this by saying I am new to Swift and IOS development. 我想通过说我是Swift和IOS开发的新手来对此进行开头。

I currently am attempting to perform a segue from a tab bar view back to the root view controller which is a split view controller. 我目前正在尝试从选项卡栏视图返回到作为拆分视图控制器的根视图控制器执行segue。 在此处输入图片说明

From the picture below I would like to select the 'cancel' button and return to my root view controller which is a split 从下面的图片中,我想选择“取消”按钮,并返回到我的根视图控制器(已拆分) 在此处输入图片说明 view controller 视图控制器

Below is the storyboard segue I set up 下面是我设置的情节提要剧集

在此处输入图片说明

Below is the code I am using. 下面是我正在使用的代码。

@IBAction func cancelButton(_ sender: Any) {
    performSegue(withIdentifier: "submit", sender: nil)

}

When I initially launch my app and begin using it. 最初启动我的应用程序并开始使用它时。 Everything works correctly. 一切正常。 As shown below when an item is selected from the left side the correct data populates in the detail view. 如下所示,当从左侧选择一个项目时,正确的数据将填充在详细视图中。

在此处输入图片说明

Even the side push menu works correctly when first opened as seen below. 初次打开时,即使侧面推动菜单也可以正常工作,如下所示。 在此处输入图片说明

However, it is when I select the 'Submit Mail' option in the side menu and attempt to return to the rootview with a segue that the app starts to break down. 但是,这是当我在侧面菜单中选择“提交邮件”选项并尝试返回到rootview时,该应用开始崩溃。

As shown below when I return with the segue I have set up the data no longer populates in the detail view when selecting an item from the master view. 如下所示,当我使用segue返回时,在从主视图中选择一个项目时,不再在详细视图中填充数据。

在此处输入图片说明

Furthermore, the side push menu is now full screen when opened. 此外,当打开时,侧面推送菜单现在全屏显示。

在此处输入图片说明

Any reason as to why this is happening? 为什么会这样呢? As a further note I am using the Cocoapod SideMenu for the side push menu. 还要注意一点,我在侧面推动菜单中使用了Cocoapod SideMenu。

Thanks! 谢谢!

Segues are used to push controllers onto the navigation hierarchy, not to go backwards, as you are attempting to do. Segues用于将控制器推入导航层次结构,而不是像您尝试的那样向后退。 You should think of your navigation hierarchy as a tree structure, where there are rarely any cyclic relationships between controllers. 您应该将导航层次结构视为树形结构,其中控制器之间几乎没有任何循环关系。 You add and remove controllers from the hierarchy to go 'forwards' and 'backwards'. 您可以从层次结构中添加和删除控制器,以进行“前进”和“后退”。 What actually happens in your scenario is that a new instance of your root controller is getting pushed onto the navigation stack, one that has not been initialised with the state that your original root controller had. 在您的方案中实际发生的情况是,将根控制器的新实例推送到导航堆栈上,而该实例尚未以原始根控制器具有的状态进行初始化。 Although it is possible to use a special kind of segue called unwind segues to go 'backwards', I would not recommend that, since they are complicated to wire, and make for code that is not explicit about what it does. 尽管可以使用一种特殊类型的segue(称为unwind segues)来“后退”,但我不建议这样做,因为它们连接起来很复杂,并且编写的代码不明确其功能。

Instead you could use UIViewController.dismiss in combination with the delegate pattern to achieve what you want. 相反,您可以将UIViewController.dismiss与委托模式结合使用以实现所需的功能。

To return from the tabBarController, I would just set an unwind segue: 要从tabBarController返回,我只需要设置一个展开键即可:

declare the unwind in the root view controller (no code needed in the func) 在根视图控制器中声明展开(在func中不需要代码)

@IBAction func unwindToRootViewController(_ sender: UIStoryboardSegue) {
    // You can print("I returned"), just to test
}

Connect the button that will trigger return to the exit button of tabBarController and select unwindToRootViewController 将将触发返回的按钮连接到tabBarController的退出按钮,然后选择unwindToRootViewController

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

相关问题 从Table View控制器到Tab Bar控制器的Segue - Segue from Table View controller to Tab Bar Controller 如何在swift中嵌入带有拆分视图控制器的标签栏控制器? - How to embed tab bar controller with a split view controller in swift? 标签栏控制器改为推送segue(快速) - Tab bar controller push segue instead (Swift) Swift 2从视图控制器返回到选项卡栏控制器中的选定选项卡 - Swift 2 Return to a selected tab in tab bar controller from view controller 查看标签栏 controller 从 storyboard swift 5 - view tab bar controller from storyboard swift 5 如何使用segue将图像从视图控制器传递到选项卡栏? - how to pass image from view controller to tab bar with segue? 故事板使用segue将数据从视图控制器传递到标签栏 - Storyboard pass data from view controller to tab bar with segue 如何在标签栏控制器中从一个视图控制器切换到另一个视图控制器并保留标签栏? - How within a tab bar controller do I segue from one view controller to another and retain the tab bar? 选择到标签栏控制器 - Segue to Tab Bar Controller Swift-从标签栏控制器更新视图控制器内的变量 - Swift - Update variable inside view Controller from Tab Bar Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM