Transferring data from one tab controller to another in fast

In my application, there are three viewController controls associated with the three tabBar tabs. I want to go from the 1st tab to the third tab and transfer some data from the 1st view controller to the 3rd controller. I cannot do this with segue, because segue creates navigation on the selected tab. This is not my requirement. I want to switch a tab to tabBar and pass some data without any navigation. How can i do this?

+7
ios uiviewcontroller swift segue uitabbar
source share
3 answers

you can use this code: target C

[tab setSelectedIndex:2]; 

save the array in NSUserDefaults as follows:

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

and retrieve data from another view using NSUserDefaults as follows:

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

speed

 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 } } 
+4
source share

Swift 3 in the latest version of Xcode 8.3.3

First, you can set the tab bar delegate UITabBarControllerDelegate to firstViewController . You can use this when you want to send data from one view controller to another by clicking the tab bar button in the 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"] } } 
+9
source share

Ok, I tried to create a single object in the viewController of the first tab, and then get this object value from the viewController of the third tabBar. It only works once when the third tab view controller is created for the first time. I never got the value of a singleton object in the third table view controller except the first time. What can i do now? In my code. In the first tab controller, if I click the button, the button tab will switch to the third tab. Below is my piece of code -

In the first tab controller -

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

In the third tab, 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, the Object Class is

 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 by:

Well, finally it works. For others who just want, like me, replace all the code from the viewDidLoad () method with viewWillAppear () on the third tab (destination tab) and it will work. Thanks.

+2
source share

All Articles