I declared a protocol in the header file of a Controller that manages a map view.
@protocol UCMapViewDelegate <NSObject>
@required
- (void)pushMapviewRight;
@end
I’m declaring the implementation of the protocol in another view controller (.h) and implement it in the .m file
// in the UCRootViewController.h
@interface UCRootViewController : UIViewController <UCMapviewDelegate>
// in the UCRootViewController.m
- (void)pushMapviewRight
{
NSLog(@"push mapview right");
}
I’m setting the delegate to a property that points to the rootviewController. This is done in the viewDidLoad() of my MapviewController, with a property @property (weak, nonatomic) id<UCMapViewDelegate> delegate;.
// in UCRootViewController
self.mapviewController.rootviewController = self;
// in UCMapViewController
self.delegate = (id<UCMapviewDelegate>)self.rootviewController;
Calling the delegated method. showMenu() gets executed when a button in the mapviewController gets pressed and it works. but the delegate method does NOT get called.
- (void)showMenu
{
NSLog(@"show menu");
[self.delegate pushMapviewRight];
}
But nothing happens.. what is wrong?! Help is greatly appreciated!
I fixed it. At first I used NSLog to verify that self was not nil (which is pretty obvious
, but still) I’m actually not sure why, but
self.mapviewController.rootviewController = self;did not “carry over” to the point where I wanted to reference self.rootViewController, although self was not nil at the point where I set it to be the pointer to rootViewController.I fixed it by creating another
initWithRootViewController:(UCRootViewController*) ctrland passed self as an argument when I created the MapViewController.Can someone explain why the valid reference to self (=rootViewController), was not available in the MapViewController?