0

Thanks in advance for your help!

In the main ViewController.m of my project I am adding a customized tableView like so:

messageController = [[MyMessagesController alloc] initWithStyle:UITableViewStylePlain];
[self addChildViewController:messageController];
[self.view addSubview:messageController.view];

Then, in the MyMessagesController.m section tableView didSelectRowAtIndexPath: I'd like to write code that would take effect in the ViewController.m where it was created and added as a childViewController.

How can I access the functions of the ViewController.m from MyMessagesController.m?

Can I make it a delegate somehow so I could call [delegate functionName];? Could I pass information back to the ViewController.m? About which of the rows in table was selected by sending through an NSString or NSArray or anything?

RanLearns
  • 4,086
  • 5
  • 44
  • 81

2 Answers2

1

Yes, use a delegate, if you are unsure how best to accomplish this, here is a good reference from Apple about delegate programming

Dan F
  • 17,654
  • 5
  • 72
  • 110
  • Yes - that is what I am looking for... how do I create this view to be a delegate for the original view so that I can interact with it? – RanLearns Jan 29 '13 at 19:03
  • You declare a delegate protocol that has the methods you need, parameters and all, declare your child VC to have a delegate that adheres to that protocol, and make sure your parent VC implements that protocol, and then set the delegate like you would for any other object that has a delegate. All of this is laid out in the link I provided in the answer – Dan F Jan 29 '13 at 19:14
0

Got it figured out, here's how you turn one viewController into a delegate for another:

In the .h of the parent controller -

@interface ViewController : UIViewController <NameOfDelegate> {

In the .m of the parent controller, once you create the new view -

newViewController.delegate = self;

and also:

- (void)functionToCall:(id)sender {
    NSLog(@"Function Called!!!");
}

In the .h of the newViewController you're adding -

@protocol NameOfDelegate;

@interface newViewController : UIViewController/OR/TableViewController {
    id <NameOfDelegate> delegate;
}

@property (nonatomic, strong) id <NameOfDelegate> delegate;

@end

@protocol NameOfDelegate
- (void)functionToCall:(id)sender;
@end

in the .m of the newViewController -

@implementation newViewController

@synthesize delegate;

and when you're ready to call the function in your delegate -

[delegate functionToCall:self];
RanLearns
  • 4,086
  • 5
  • 44
  • 81