Creating a delegate for a UIViewController

I looked through everything and found a lot of people with similar problems, but I still can't get my delegates to work. I want to create a model view manager, and then call a method in the view that made the model look to reject it. So, I have this line:

mergeConfig *view = [[mergeConfig alloc] initWithNibName:@"mergeConfig" bundle:nil]; 

and I'm trying [view setDelegate:self]; as stated on the Apple developer pages , but as expected, in my view of the model there is no setDelegate method.

So I want to know how I can get it to set a delegate? And as soon as I do this, it just automatically passes calls to methods on methods in the parent view with the same name? Apple pages do not indicate which code to enter into the model view controller.

+4
source share
2 answers

You need to define a delegate on your custom view controller, for example:

 @interface mergeConfig { id delegate; } @property (nonatomic, assign) id delegate; @end @implementation mergeConfig @synthesize delegate; @end 

Then, in another place in the class for your view controller, you can reference any methods that you need for your delegate.

Personally, I like to improve the above by defining the protocol that my delegates execute, as follows:

 @protocol MyDelegateProtocol - (void)delegateMethod; @end @interface mergeConfig { id<MyDelegateProtocol> delegate; } @property (nonatomic, assign) id<MyDelegateProtocol> delegate; @end @implementation mergeConfig @synthesize delegate; @end 
+5
source

If you just need to reject the modal view controller, just call [self.parentViewController dismissModalViewControllerAnimated:YES]; at the appropriate time. There is no need for delegates unless you need to pass information to the chain.

0
source

All Articles