0

I have SendingController which push to nav stack SendingDeatilsController (which one has a TableView). User should should pick in TableView one row (it checked by Checkmark) and I would like to pass the value of this row (let it will NSString object) to the SendingController.

How can I realize this behaviour in my application? And is SendingController parent for SendingDetailController (attribute parentController of SDC refers to SC) ??

LIAL
  • 1,624
  • 4
  • 24
  • 30

2 Answers2

1

If you want to implement this behaviour, pass the SendingDetailController a reference to the previous view controller. This way the detail view controller can send a message to the previous one on the stack.

In your SendingDetailController define a weak reference :

// in .h
SendingController *sendingController;
@property(assign) SendingController *sendingController;

// in .m
@synthesize sendingController;

-(void)tableView:(UITableView *)tableView
 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // retrieve the string and send the message
    [sendingController didSelectString:theString];
}

Now before pushing the SendingDetailController on the stack don't forget to set its sendingController property.

// .m
// where you push the vc
if(!sendingDetailController) {
    sendingDetailController = [[SendingDetailController alloc]
                               initWithNibName:@"TheNIBName"
                                        bundle:nil];
    sendingDetailController.sendingController = self;
}
[self.navigationController pushViewController:sendingDetailController
                                     animated:YES];

and write the method that will recieve the string.

-(void)didSelectString:(NSString *)aString {
    // do anything with string
    [self.navigationController popViewControllerAnimated:YES];
}

This should do the job.

  • Should I release sendingController property in dealloc of SendingDetailController ?? – LIAL Jul 23 '11 at 13:10
  • @LIAL - no, as you can see in the property definition, the value is just assigned, not retained. this is a weak reference. –  Jul 23 '11 at 13:11
0

For easy asynchronous communication between different UIViewControllers, you may want to look at NSNotification and NSNotificationCenter.

There are plenty tutorials on the web and some good answers here at SO that can show you how to do that exactly.

Peter Sarnowski
  • 11,900
  • 5
  • 36
  • 33