1

I have ViewController which perform model type segue and another UITableViewController comes from bottom and it has Categories in UITableView. I want that when any of the category selected it must pass back some data to the sender controller

brainray
  • 12,512
  • 11
  • 67
  • 116

2 Answers2

2

Set the first view controller as a custom delegate for the modal UITableViewController, you can get a reference to the UITableViewController or the UITableView in the first view controller in this method as so:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"tableviewController"]){
         MyTableViewController *myTableViewController = (MyTableViewController *)[segue      destinationViewController];

         myTableViewController.delegate = self;
     }
} 

You will need to set up the delegate callbacks etc and a property on the table view controller. There should be plenty of guides on how to do this if you dont already know, here is one

How do I set up a simple delegate to communicate between two view controllers?

Community
  • 1
  • 1
Sam Clewlow
  • 4,293
  • 26
  • 36
  • when i check my delegate it gets null. even i am assigning child view controller's delegate property to self (parent) but when in child controller i check it. its null – Usman Ahmad Oct 06 '12 at 14:02
  • hmmm, I would breakpoint the if statement, and check that the condition is not failing for some reason. Double check the segue identifier is the same in the storyboard as the on you are checking for. – Sam Clewlow Oct 07 '12 at 12:54
1

If this is something your user would potentially do repeatedly without dismissing the view, your best bet is probably to post a notification, have sender register to listen for that.

To post a notification do this, whenever a category is changed:

 NSNotification *aNotification = [NSNotification notificationWithName:categoryChangedNotification object:categoryThatWasChanged];
[[NSNotificationCenter defaultCenter] postNotification:aNotification];

To listen for it in the sender:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_refreshCategories) name:categoryChangedNotification object:nil];

Remember to addObserver and removeObserver appropriately so as not to be observing when unnecessary.

If user selects and then dismisses the view, returning to the sender view, you'd be better off making a protocol, set the sender as delegate. Essentially, a protocol is created in your .h file like so:

 @protocol myControllerDelegate
      -(void)myControllerFinishedEditingCategories:(id)sender;
 @end

Then you need a property in same controller:

 @property (nonatomic, unsafe_unretained) id<myControllerDelegate> delegate;

Make the sending view conform to the protocol:

 @interface sendingViewController : UIViewController <myControllerDelegate>

Now when you finish editing the categories you can call the delegate method on the sender before dismissing your view:

 [delegate myControllerFinishedEditingCategories:self];
Dean Davids
  • 4,174
  • 2
  • 30
  • 44
  • P-Double gives you the correct manner in which to set your sending view as delegate. You'll need to do that as well. – Dean Davids Oct 05 '12 at 11:10