Search code examples
iosswiftuiviewcontrollersegueuitabbar

passing data from one tab controller to another in swift


My application has three viewController associated with three tab of tabBar. I want to switch from 1st tab to 3rd tab and pass some data from 1st view controller to 3rd view controller. I can't do it with segue, because segue create navigation within the selected tab. That is not my requirement. I want to switch tab in tabBar and pass some data without any navigation. How can i do it ?


Solution

  • you can use this code : Objective C

    [tab setSelectedIndex:2];
    

    save your array in NSUserDefaults like this:

    [[NSUserDefaults standardUserDefaults]setObject:yourArray forKey:@"YourKey"];
    

    and get data from another view using NSUserDefaults like this :

    NSMutableArray *array=[[NSUserDefaults standardUserDefaults]objectForKey:@"YourKey"];
    

    swift

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
          if segue.identifier == "toTabController" {
            var tabBarC : UITabBarController = segue.destinationViewController as UITabBarController
            var desView: CaseViewController = tabBarC.viewControllers?.first as CaseViewController
    
            var caseIndex = overviewTableView!.indexPathForSelectedRow()!.row
            var selectedCase = self.cases[caseIndex]
    
            desView.caseitem = selectedCase
          }
        }