Search code examples
objective-ciosxcodeuisplitviewcontroller

Updating masterview from detail view


I am working with a split view controller with a master and detail view. I have a view on my detail view controller that contains a user input field which should call a method on the master view controller to update an array. I am having trouble setting up the communication between the two sides. I have tried to simply call that method using [masterViewController updateCalcs], but that doesnt seem to work and errors out with the below:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-
[UINavigationController updateCalcs]: unrecognized selector sent to instance 0xb08d130'

Can anyone help me out by some sample code or a source to explain how exactly to call a method on one view from the other?


Solution

  • The best practice is to use delegates. In your detailview.h add:

    @protocol TestDelegate <NSObject>
    -(void)doSomeThing;
    @end
    
    @property (nonatomic, weak) id <TestDelegate> delegate;
    

    In detailview.m:

    [self.delegate doSomeThing];
    

    In masterview.m after the detailview is created add:

    detailView.delegate = self;
    

    In masterview.h add the delegate like:

    @interface MasterView : UIViewController <TestDelegate> {