0

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()
    }
}
Jay Patel
  • 2,642
  • 2
  • 18
  • 40
CoderJ13
  • 117
  • 1
  • 13
  • 1
    not related to your issue but it should be `super.viewDidAppear(animated)` – Leo Dabus Jan 06 '18 at 00:11
  • And there is no need for `UserDefaults.standard.synchronize()`. – rmaddy Jan 06 '18 at 00:12
  • @sweepez if you look at the title it is pretty much the same question – Leo Dabus Jan 06 '18 at 00:21
  • @LeoDabus I saw this solution but didn't work since NotificationCenter.default.addObserver is set at the viewdidload that isn't called after segmented view change. – CoderJ13 Jan 06 '18 at 00:27
  • @sweepez I got this error Ambiguous reference to member 'tableView(_:numberOfRowsInSection:) when using the code controller.tableView.reloadData() in the first view controller – CoderJ13 Jan 06 '18 at 00:31
  • @CoderJ13 so edit your question and show your attempt where you add the observer and post the notification to reload the data. – Leo Dabus Jan 06 '18 at 00:35
  • @sweepez I got an error type "my controller name" has no member tableview when using the code controller.tableView.reloadData() in the first view controller – CoderJ13 Jan 06 '18 at 00:37
  • @LeoDabus where should I add `.valueChanged .addTarget(self, action: #selector(segmentedControlValueChanged), for: .valueChanged) ` in viewedload ? – CoderJ13 Jan 06 '18 at 00:42
  • @sweepez used code `let myController = controller as! HomeTableViewController mycontroller.tableView.reloadData()` and the error was used of the unresolved identifier "mycontroller" – CoderJ13 Jan 06 '18 at 00:45
  • @sweepez used this `let myController = controller as! HomeTableViewController mycontroller.tableView.reloadData()` as my real code and HomeTableViewController is the controller that includes the tableview that I want to refresh – CoderJ13 Jan 06 '18 at 00:55
  • @sweepez ohh thank you for your help the code `let myController = controller as! HomeTableViewController myController.tableView.reloadData()` runned without errors but still it didn't reloaded the .php in the second view controller so the data stayed the same is there a way I can call the get() function that loads the php from `if index == 0 { // code }` – CoderJ13 Jan 06 '18 at 01:03
  • @sweepez I am updating the php that is being loaded with the get() function above in my question. when I pull to refresh (that runs the get() ) the table view update the data correctly – CoderJ13 Jan 06 '18 at 01:07
  • @CoderJ13 I don’t know if you can use valueChanged control event with your SJSegmemted. I thought you were using UISegmentedControl – Leo Dabus Jan 06 '18 at 01:07
  • @sweepez I am updating the php that contains the data with this line UserDefaults.standard.set(“xyz.php”, forKey: "DealsPHP"). and being loaded in get() – CoderJ13 Jan 06 '18 at 01:08
  • @sweepez my table view is only being updated when I run the get() function is there a way I can call this function from `if index == 0 { // code }` – CoderJ13 Jan 06 '18 at 01:10
  • @CoderJ13 we are spamming the comment sections, come here and we can chat if you want `https://tlk.io/coderj13` – sweepez Jan 06 '18 at 01:13
  • @sweepez calling the get() function at viewdidappear or viewdidload is only called one time but when I change view with the segmented controller view didload and viewdidappear are not called since it is the same viewcontroller. So is there a way I can call the get() function from func didMoveToPage so that every time a segment change the get() function is called – CoderJ13 Jan 06 '18 at 01:14

1 Answers1

0

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()
CoderJ13
  • 117
  • 1
  • 13