Question

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?

Was it helpful?

Solution 2

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];

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top