简体   繁体   中英

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.

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. 在此处输入图片说明

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.

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.

在此处输入图片说明

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.

Thanks!

Segues are used to push controllers onto the navigation hierarchy, not to go backwards, as you are attempting to do. 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.

Instead you could use UIViewController.dismiss in combination with the delegate pattern to achieve what you want.

To return from the tabBarController, I would just set an unwind segue:

declare the unwind in the root view controller (no code needed in the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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