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.
Nuibb
source share