简体   繁体   中英

Tab Bar Controller - change data if segue was initiated from tab bar

I'm trying to do something which seems simple, but I am having difficulty. I have a tab bar application with a setting page which is a table view. The user can change the settings by selecting a row, which pushes a new table view with new setting options. When the user selects a new setting, I send the information back to the main table view and populate the table view with the new setting.
The problem : I also have a SAVE button. If the user does not save the new settings I want to discard them. If the user selects a new tab (without saving settings) and then selects the settings tab, the last data the user entered is still in the tableView. I understand this is because viewdidload is not called again after the view has been created.

Basically I want logic like this :

If segue was initiated by clicking a tab bar icon : load the table view using dataModel.
Else if the previous screen was the data entry screen : Load the table view using user selected data.

If I wasn't using a tab bar controller I would do this by loading the dataModel using viewdidload and loading the tableview using delegate methods. The problem is I can't use viewdidload. I also can't use viewwillappear because it is called both when opening the screen from the table view and when popping the entry view controller off the stack. 在此处输入图片说明

I tried to set up the delegate method for the Tab Bar Controller in AppDelegate

 func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
        if (tabBarController.selectedIndex == 2)
        {   let navController = viewController as! UINavigationController
            let settingsTableViewController = navController.viewControllers[0] as! SettingsTableViewController
            settingsTableViewController.loadFromDataModel = true
            println("In did select view controller in app delegate")
        }     
    }

And if loadFromDataModel = true then I load from the data Model in the Settings Table View Controller. Here is the problem - this only works if I go back and forth from the tabs twice. So weird. I put in println statements and it seems like execution is happening in the following sequence:

  1. ViewWillAppear is being called in SettingsTableViewController

  2. tabBarController:didSelectViewController is being called from the AppDelegate (and the variable loadFromDataModel is updated - but it is too late because viewwillappear has already been called)

In Summary : Is this the best way to determine if the segue came from the tab bar? Why is viewwillappear on my SettingsTableViewController being called before the delegate method? Any suggestions on how to load the data from the data model each time the user selects the tab via the tab bar. Am I missing some obvious method? Thanks for any help!

By the time didSelectViewController is called, it's too late to do what you are looking to do. As you can tell by the viewDWillAppear delegate firing. Try moving your code to the shouldSelectViewController delegate of the tabBarController That should be early enough in the process to set your data.

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