I have a first viewcontroller that includes a segmented controller (SJSegmented Controller). It comes with a function where I can know the user is at which segment and change the php that loads the data of the other view controller but when I get to the view controller that includes the table view the Viewdidload and Viewdidappear is only performed one time and after the user change the segment the Viewdidload and Viewdidappear are not loaded.
Here is the code where from the first view controller that is the segmented controller.
func didMoveToPage(_ controller: UIViewController, segment: SJSegmentTab?, index: Int) {
if selectedSegment != nil {
selectedSegment?.titleColor(.gray)
}
if segments.count > 0 {
selectedSegment = segments[index]
}
// Checks witch segment the user is at and change the php to load accordingly
let CurrSeg = index
print(CurrSeg)
let Curr = String(CurrSeg)
if Curr == "0" {
UserDefaults.standard.set(“xyz.php”, forKey: "DealsPHP")
UserDefaults.standard.synchronize()
} else if Curr == "1" {
// UserDefaults.standard.set("hwy.php", forKey: "DealsPHP")
// UserDefaults.standard.synchronize()
} else if Curr == "2" {
UserDefaults.standard.set("uys.php", forKey: "DealsPHP")
UserDefaults.standard.synchronize()
} else if Curr == "3" {
UserDefaults.standard.set("wrt.php", forKey: "DealsPHP")
UserDefaults.standard.synchronize()
}
}
I want when the didMoveToPage is executed to reload the table view of the other view controller.
Code for second view controller (that includes the table view)
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableView.reloadData()
}
PS: when didMoveToPage is executed It change the php and load the same view controller with the changed php.
Here is the code that populates the table view:
func get() {
let DealsPHP: String = (UserDefaults.standard.object(forKey: "DealsPHP") as? String)!
let url = URL(string: DealsPHP)
let data = try? Data(contentsOf: url!)
if data == nil {
// Create the alert controller
let alertController = UIAlertController(title: "No Internet Connection", message: "It looks like you aren't connected to the Internet. Please check your internet connection and try again.", preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "Try Again", style: UIAlertActionStyle.default) {
UIAlertAction in
self.get()
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
} else {
self.values = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
self.tableView.reloadData()
}
}
This question was solved by @sweepez. I added this code in if Curr == "1" {} ** at **func didMoveToPage this reloaded the data of the tableview evreytime the segment changed
let myController = controller as! YourControllerName
myController.get()