0

I've a table view with navigation controller embedded in. I've added a UIBarButtonItem (add) button. When I click this button it opens a new view where user enters the data and submits it (which makes a web service call) and returns back to the previous view. This navigation happens as shown below,

func addTapped(_ sender:UIBarButtonItem) {
     print("Called Add")
     let vc = (storyboard?.instantiateViewController( withIdentifier: "newNote")) as! newNoteVC
     self.navigationController?.pushViewController(vc, animated: true)
}

And in new view I do following,

@IBAction func saveButton(_ sender: UIButton) {
    if (self.noteDescription.text?.isEmpty)! {
        print("Enter missing note description")
        return
    } else {
        let desc = self.noteDescription.text
        self.uploadNote(noteText: desc!, noteDate: self.dateInMilliseconds)
        self.navigationController?.popViewController(animated: true)
    }
}

This way a record gets saved and a view gets popped from the navigation controller stack but only thing I don't how to do is refresh the table view data in the parent view (where I might need to make a new http service call to read all the records).

I hope I'm able to explain the issue? Any help is appreciated.

toddg
  • 2,863
  • 2
  • 18
  • 33
Atarang
  • 422
  • 1
  • 6
  • 22
  • you need refresh a table in currentViewController or in previous one? – Reinier Melian Sep 12 '17 at 16:41
  • 2
    I would suggest using protocol/delegate pattern to pass the new note back to your `UITableViewController`. See here: https://stackoverflow.com/questions/30618172/how-to-send-data-back-by-popviewcontrolleranimated-for-swift – toddg Sep 12 '17 at 16:44
  • Seems like overkill to make an http request to get data when you could simply pass the one record back and add it to your table view – toddg Sep 12 '17 at 16:46
  • @Rainier, So controllerOne is a tableviewcontroller and controllerTwo is a simple view controller to add new Note info and submit. That mean controllerOne needs to do refresh or reload but after controllerTwo returns successfully. Due to navigation bar, user can just click back-button (arrow) which mean no refresh necessary. But after successful 'Save' we must do refresh. – Atarang Sep 12 '17 at 21:44
  • @toddg That was a good suggestion and it worked seamlessly. Thanks. – Atarang Sep 13 '17 at 06:20

1 Answers1

0

As mentioned in the comments, making a service call just to update the tableview might be a overkill. However, if this is the business scenario which needs to be implemented, you can do the same in

func viewWillAppear

in the parent view controller. You can make the service call in this method and reload the table view with the data. You would also need to check the overall navigation of the application as making service calls in viewWillAppear method is not a good approach as these gets called everytime the view is shown. For Ex: If coming back from a navigated view then also the method is called.