0

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?

John Baum
  • 3,183
  • 11
  • 42
  • 90
  • It looks like you're calling the `updateCalcs` on the NavigationController, I think masterViewController should be `[self.navigationController.viewControllers objectAtIndex:0].visibleViewController` – mkral Dec 14 '12 at 15:44

1 Answers1

0

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> {
Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
  • I was thinking of using a notification message. Do you think thats not the best way to go about things? – John Baum Dec 14 '12 at 15:25
  • I think delegates are better programming practice, see http://stackoverflow.com/questions/2232694/delegates-vs-notifications-in-iphoneos for more info – Roland Keesom Dec 14 '12 at 15:26
  • I think you should use MVC and have your array stored in a model, in your detail view controller update the model then add a notification observer for when that array changes on the master view. – mkral Dec 14 '12 at 15:40
  • I actually have multiple view controllers being pushed onto the detail side and many of those require the recalc call to the master view. In this case, would delegates still apply? – John Baum Dec 14 '12 at 15:57
  • I don't know what you are actually updating and sending between these. But in my opinion delegates work fine, also for multiple viewcontrollers. Just implement the delegate in your masterView.h file. And use them like in the example. – Roland Keesom Dec 14 '12 at 16:04