Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Delegating is a good programming practice for many situations but that doesn't mean you have to use it if you're not comfortable with it. Both delegating and notifications help decouple the view controllers from each other, which is a good thing. Notifications might be a little easier to code and offer the advantage that multiple objects can observe one notification. With delegates, such a thing cannot be done without modifying the delegating object (and is unusual).</p> <p>Some advantages of delegating:</p> <ul> <li>The connection between delegating object and delegate is made clearer, especially if implementing the delegate is mandatory.</li> <li>If more than one type of message has to be passed from delegatee to delegate, delegating can make this clearer by specifying one delegate method per message. <del>For notifications, you can use multiple notification names but all notifications end up in the same method on the side of the observer (possibly requiring a nasty switch statement).</del></li> </ul> <p>Only you can decide what pattern is more appropriate for you. In any case, you should consider not having your view controller send the notification or the delegate message. In many cases, the view controller should change the model and then the model should inform its observers or its delegate that it has been changed.</p> <p>Implementing a delegate pattern is simple:</p> <ol> <li><p>In your ChildViewController.h, declare the delegate protocol that the delegate must implement later:</p> <pre><code>@protocol ChildViewControllerDelegate &lt;NSObject&gt; @optional - (void)viewControllerDidChange:(ChildViewController *)controller; @end </code></pre></li> <li><p>At the top of the file, create an instance variable to hold the pointer to the delegate in your ChildViewController:</p> <pre><code>@protocol ChildViewControllerDelegate; @interface ChildViewController : UIViewController { id &lt;ChildViewControllerDelegate&gt; delegate; ... } @property (assign) id &lt;ChildViewControllerDelegate&gt; delegate; ... @end </code></pre></li> <li><p>In RootViewController.h, make your class conform to the delegate protocol:</p> <pre><code>@interface RootViewController : UIViewController &lt;ChildViewControllerDelegate&gt; { ... </code></pre></li> <li><p>In the RootViewController implementation, implement the delegate method. Also, when you create the ChildViewController instance, you have to assign the delegate.</p> <pre><code>@implement RootViewController ... // in some method: ChildViewController *controller = [[ChildViewController alloc] initWithNibName:... controller.delegate = self; ... - (void)viewControllerDidChange:(ChildViewController *)controller { NSLog(@"Delegate method was called."); } ... </code></pre></li> <li><p>In the ChildViewController implementation, call the delegate method at the appropriate time:</p> <pre><code>@implementation ChildViewController ... // in some method: if ([self.delegate respondsToSelector:@selector(viewControllerDidChange:)]) { [self.delegate viewControllerDidChange:self]; } ... </code></pre></li> </ol> <p>That's it. (Note: I have written this from memory so there are probably some typos/bugs in it.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload