Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Simple example...</p> <p>Let's say the child view controller has a <code>UISlider</code> and we want to pass the value of the slider back to the parent via a delegate.</p> <p>In the child view controller's header file, declare the delegate type and its methods:</p> <p><strong>ChildViewController.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; // 1. Forward declaration of ChildViewControllerDelegate - this just declares // that a ChildViewControllerDelegate type exists so that we can use it // later. @protocol ChildViewControllerDelegate; // 2. Declaration of the view controller class, as usual @interface ChildViewController : UIViewController // Delegate properties should always be weak references // See http://stackoverflow.com/a/4796131/263871 for the rationale // (Tip: If you're not using ARC, use `assign` instead of `weak`) @property (nonatomic, weak) id&lt;ChildViewControllerDelegate&gt; delegate; // A simple IBAction method that I'll associate with a close button in // the UI. We'll call the delegate's childViewController:didChooseValue: // method inside this handler. - (IBAction)handleCloseButton:(id)sender; @end // 3. Definition of the delegate's interface @protocol ChildViewControllerDelegate &lt;NSObject&gt; - (void)childViewController:(ChildViewController*)viewController didChooseValue:(CGFloat)value; @end </code></pre> <p>In the child view controller's implementation, call the delegate methods as required.</p> <p><strong>ChildViewController.m</strong></p> <pre><code>#import "ChildViewController.h" @implementation ChildViewController - (void)handleCloseButton:(id)sender { // Xcode will complain if we access a weak property more than // once here, since it could in theory be nilled between accesses // leading to unpredictable results. So we'll start by taking // a local, strong reference to the delegate. id&lt;ChildViewControllerDelegate&gt; strongDelegate = self.delegate; // Our delegate method is optional, so we should // check that the delegate implements it if ([strongDelegate respondsToSelector:@selector(childViewController:didChooseValue:)]) { [strongDelegate childViewController:self didChooseValue:self.slider.value]; } } @end </code></pre> <p>In the parent view controller's header file, declare that it implements the <code>ChildViewControllerDelegate</code> protocol.</p> <p><strong>RootViewController.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "ChildViewController.h" @interface RootViewController : UITableViewController &lt;ChildViewControllerDelegate&gt; @end </code></pre> <p>In the parent view controller's implementation, implement the delegate methods appropriately.</p> <p><strong>RootViewController.m</strong></p> <pre><code>#import "RootViewController.h" @implementation RootViewController - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ChildViewController *detailViewController = [[ChildViewController alloc] init]; // Assign self as the delegate for the child view controller detailViewController.delegate = self; [self.navigationController pushViewController:detailViewController animated:YES]; } // Implement the delegate methods for ChildViewControllerDelegate - (void)childViewController:(ChildViewController *)viewController didChooseValue:(CGFloat)value { // Do something with value... // ...then dismiss the child view controller [self.navigationController popViewControllerAnimated:YES]; } @end </code></pre> <p>Hope this helps!</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