-1

I have a searchViewController where I search for users and UITableView gets updated dynamically with user information. The cell for the UITableView is custom - it has a UIImage, the usernameLabel, and a button called "Add".

What I want is that if the user clicks on the add button of the cell, it should pass the user information on that cell (image and username) to another view controller that has a UITableView that is a friend list.

However, so far the only way I know is by using performSegue to pass the data on to the other viewController holding the friendlist UITable. But by this method, every time I click the add button it segues to the other view controller which I don't want. I want it to stay on the searchViewController when the add button is clicked - I only want the data to be passed.

Is there any way I can do this? Is using NSUserDefaults advisable for passing data of this sort?

Anitej Rao
  • 25
  • 10
  • does the other view controller exist when you click on the add button? i.e. is the friend list a new screen that you will be navigating to or a previous screen that you came from and can go back to? – Rajeev Bhatia Apr 04 '17 at 10:01
  • you can easily create a method in a cell and pass the information. – Anil Kukadeja Apr 04 '17 at 10:02
  • Why are you sending data to friendlist screen if you do not want the friend list to show ? Do you want to use some functionality of it ? – NeverHopeless Apr 04 '17 at 10:03
  • Use closure and update your tableview. – dahiya_boy Apr 04 '17 at 10:04
  • So this is how it is - I have the friendsList view controller with a bar button item that takes me to the addFriend view controller that has the uitableview with the custom cell. The add button on the custom cell is used to update the tableView on the friendsList view controller. I do not want to segue immediately after clicking on the add button incase the user wants to add more users to the list. But the user can segue back to the friendsList controller via another button when they want to. – Anitej Rao Apr 04 '17 at 10:13
  • @RajeevBhatia the friend list is a viewcontroller that I have come from via a bar button item. And hence can go back to via a bar button item too. Hope that helps! – Anitej Rao Apr 04 '17 at 10:20
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – NSNoob Apr 04 '17 at 10:45
  • It would be wrong to send them one by one. Then the best solution would be to add the "friend" details to an Array of Friends when the user clicks add. Then when the user is done. Perform the segue and pass the array. So you have all the friends the user added – Devster101 Apr 04 '17 at 10:59

4 Answers4

0

For simplicity I will use FriendListVC and AddVC

If you are going to your AddVC from FriendListVC via a bar button item or something and your stack looks like:-

FriendListVC -> AddVC

There are two approaches you can use:-

1) Create a delegate of your friendListVC in your addVC and modify the friendListVC datasource on any changes there

2) Or, and I recommend this approach, just reload your FriendListVC datasource on it's viewWillAppear. viewWillAppear will get called even if you navigate back. Thus even if you add a deleteVC in the future and navigate back, the viewWillAppear will perform the updates and it will be independent of any other VC

Hope that helps

Rajeev Bhatia
  • 2,974
  • 1
  • 21
  • 29
  • Hi so would I implement viewWillAppear in the addVC (because that's where all the information is that I want to pass on to FriendListVC), sorry I'm not familiar with viewWillAppear. Also, what exactly do you mean by deleteVC? – Anitej Rao Apr 04 '17 at 10:41
  • implement viewWillAppear in your friendListVC and reload the data for the table i.e. your datasource here). deleteVC was just an example of another VC that you might add in the future – Rajeev Bhatia Apr 04 '17 at 10:43
0

Use delegate for passing data between view controllers. you can find this useful Passing data between 2 UIViewController using delegate and protocol you can use NSUserDefaults but delegate pattern is better than this.

Community
  • 1
  • 1
elk_cloner
  • 2,049
  • 1
  • 12
  • 13
0

You can use callback method best and easy way to pass data one controller to another

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{
    let viewControllerB = segue.destinationViewController as! ViewControllerB
    viewControllerB.callback = { message in
    //Do what you want in here!
    }
}

In ViewControllerB:

var callback : (String -> Void)?
@IBAction func search(sender: AnyObject) {
    callback?("Pass data to view controller1")
    self.dismissViewControllerAnimated(true, completion: nil)
}
Arvind Kumar
  • 2,371
  • 1
  • 18
  • 25
0

The easiest way to do this is by making an instance of the view controller that you want to pass data to, in the current view controller. I will write you a sample code for this.

class yourTableViewController: UITableViewController {
  var controllerToPassData: UIViewController()

  func clickTableButton(sender: UIButton) {
    controllerToPassData.count += 1
  }
}

class controllerwhereDataisPassed: UIViewController {
  var count: Int!
}

Pick the instance of the controller where you want to pass data to from the navigationController stack and use this code.

CoderSulemani
  • 123
  • 11
  • did you use the same instance of the friends view controller that is there in the navigation stack? And did you reload the table view? – CoderSulemani Apr 06 '17 at 08:51