Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 928
Nuibb Avatar asked Feb 11 '15 12:02

Nuibb


3 Answers

Swift 3 in latest Xcode version 8.3.3

First you can set a tab bar delegate UITabBarControllerDelegate in FirstViewController. You can use this when you want to send data from one view controller to another by clicking the tab bar button in UITabBarViewController.

class FirstViewController: UIViewController ,UITabBarControllerDelegate {
    let arrayName = ["First", "Second", "Third"] 

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tabBarController?.delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController.isKind(of: FirstsubsecoundViewController.self as AnyClass) {
            let viewController  = tabBarController.viewControllers?[1] as! SecondViewController
            viewController.arrayData = self.arrayName
        } 

        return true
    }
}

Get data in SecondViewController:

class SecondViewController: UIViewController {
    var arrayData: [String] = NSArray()
    override func viewDidLoad() {
       super.viewDidLoad()
       print(arrayData) // Output: ["First", "Second", "Third"]
   }
}
like image 59
Arjun Yadav Avatar answered Sep 22 '22 08:09

Arjun Yadav


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
      }
    }
like image 29
Jay Bhalani Avatar answered Sep 22 '22 08:09

Jay Bhalani


Okay, i tried with creating a singleton object in viewController of first tab and then get that object's value from viewController of third tabBar. It works only for once when third tab's view controller instantiates for the 1st time. I never got that singleton object's value in third tab's view controller except the first time. What can i do now ? In my code - In first tab's controller, if i click a button tab will be switched to third tab. Below is my code portion -

In First tab's controller -

@IBAction func sendBtnListener(sender: AnyObject) {
        Singleton.sharedInstance.brandName = self.searchDisplayController!.searchBar.text
        self.tabBarController!.selectedIndex = 2
}

In Third tab's Controller -

 override func viewDidLoad() {
     super.viewDidLoad()

     //Nothing is printed out for this portion of code except the first time 
     if !Singleton.sharedInstance.brandName.isEmpty{
         println(Singleton.sharedInstance.brandName)

     }else{

         println("Empty")
     }
 }

In Singleton Object's class -

class Singleton {
    var name : String = ""
    class var sharedInstance : Singleton {
        struct Static {
            static let instance : Singleton = Singleton()
        }
        return Static.instance
    }

    var brandName : String {
        get{
            return self.name
        }

        set {
            self.name = newValue
        }
    }
} 

Edited :

Okay at last it's working. For others who just want like me, please replace all the code from viewDidLoad() to viewWillAppear() method in third tab's (destination tab) controller and it will work. Thanks.

like image 20
Nuibb Avatar answered Sep 26 '22 08:09

Nuibb