Note that there are some explanatory texts on larger screens.

plurals
  1. POobjective C: Using a Delegate to call a function in parent class
    text
    copied!<p>I'm creating a 3 layer navigation popup controller and on the 3rd popup controller I have a delegate method to access dismissPopup method that is in the parent class. I can't seem to call it, my NSLog messages in the function in the parent class isn't even showing so I must be either using delegation wrong or I'm calling it incorrectly. </p> <p>The 3 classes ParentViewController has a toolbar with a button that brings up the table view --> RegionViewController is the First table view controller with items --> ConusViewController is the 2nd table view controller that is pushed onto the navigation stack. I'm trying to call the method dismissPopover that is in the parent method with a delegation after the selection is clicked on so the whole popover goes away.</p> <p>In the ConusViewController if the delegation had worked I would have seen "Method Accessed" from the function in the parent class. It doesn't show so I must be using delegation wrong.</p> <p>Sorry for being so wordy on my post, I wanted to be complete on what I'm trying to do here. Thanks.</p> <p><em><strong>ParentViewController.h</em></strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "ConusViewController.h" @interface EnscoWXViewController : UIViewController &lt;ConusViewControllerDelegate&gt; { UIPopoverController *popoverController; IBOutlet UIWebView *webImageDisplay; ConusViewController *cViewController; } @property (nonatomic, retain) UIPopoverController *popoverController; @property (nonatomic, retain) UIWebView *webImageDisplay; @property (nonatomic, retain) ConusViewController *cViewController; -(IBAction) buttonShowRegion:(id) sender; @end </code></pre> <p><em><strong>ParentViewController.m</em></strong></p> <pre><code>#import "ParentViewController.h" #import "RegionViewController.h" @implementation ParentViewController @synthesize cViewController; -(IBAction) buttonShowRegion:(id) sender { ... } -(void)dismissPopover { [popoverController dismissPopoverAnimated:YES]; printf("Method Accessed\n"); } - (void)viewDidLoad { cViewController = [[ConusViewController alloc] init]; cViewController.delegate = self; [super viewDidLoad]; } </code></pre> <p><em><strong>RegionViewController.m</em></strong></p> <pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.row) { case 0: { ConusViewController *conusViewController = [[ConusViewController alloc] initWithNibName:@"ConusViewController" bundle:nil]; conusViewController.contentSizeForViewInPopover = CGSizeMake(320, 350); [self.navigationController pushViewController:conusViewController animated:YES]; [conusViewController release]; break; } case 1: { break; } } } </code></pre> <p><em><strong>ConusViewController.h</em></strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @protocol ConusViewControllerDelegate &lt;NSObject&gt; @required - (void)dismissPopover; @end @interface ConusViewController : UITableViewController { NSMutableArray *conusItems; id delegate; } @property (nonatomic, assign) id &lt;ConusViewControllerDelegate&gt; delegate ; @end </code></pre> <p><em><strong>ConusViewController.m</em></strong></p> <pre><code>#import "ConusViewController.h" #import "ParentWXViewController.h" @implementation ConusViewController @synthesize delegate; - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *itemRequested = [conusItems objectAtIndex:indexPath.row]; NSLog(@"logging: %@", itemRequested); [delegate dismissPopover]; [itemRequested release]; } </code></pre>
 

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