How do you call a parent/delegate method from a child view?


I been struggling with this problem for 2 days but got nowhere. How do you call a parent/delegate method from a child view/UIPopoverController? Can somebody please help? Is this not possible? I’m also thinking of using a block function to accomplish this but no luck…:open_mouth:
Here are my code:

I created a method in BNRDetailViewController called setTitle. This method is used to update the UIBarButton title and is also the method I want to call from the BNRAssetTypeViewController class (Popover class).


... //*ADDED @property (weak, nonatomic) IBOutlet UIBarButtonItem *assetTypeButton; //*ADDED -(void)setTitle:(NSString *)title;


#import “BNRAssetTypeViewController.h”

@property (strong, nonatomic) UIPopoverController *itemTypePickerPopover;


@implementation BNRDetailViewController



self.assetTypeButton.title = title;



  • (IBAction)showAssetTypePicker:(id)sender {

    [self.view endEditing:YES];

    BNRAssetTypeViewController *avc = [[BNRAssetTypeViewController alloc]init];

    avc.item = self.item;

    //*ADDED <-------------------------

    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {

      self.itemTypePickerPopover = [[UIPopoverController alloc] initWithContentViewController:avc];
      self.itemTypePickerPopover.delegate = self;   //<--------!!!!!!HERE IS THE DELEGATE ASSIGNMENT!!!!!
      [self.itemTypePickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];


      [self.navigationController pushViewController:avc animated:YES];




and Here is the header and implementation for BNRAssetTypeViewController:


#import <Foundation/Foundation.h>

@class BNRItem;

@protocol BNRDetailViewController <NSObject>

-(void)setTitle:(NSString *)title;


@interface BNRAssetTypeViewController : UITableViewController

@property (nonatomic, strong)BNRItem *item;

@property (nonatomic,weak) id delegate;


and the implementation file:


#import "BNRDetailViewController.h"

@implementation BNRAssetTypeViewController


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    NSArray *allAssets = [[BNRItemStore sharedStore]allAssetTypes];
    NSManagedObject *assetType = allAssets[indexPath.row];
    self.item.assetType = assetType;

     NSString *assetLabel = [self.item.assetType valueForKey:@"label"];
    NSLog(@"************* : %@", assetLabel);
    [self.delegate setTitle:assetLabel];  //NOTE: CALLING THE DELEGATE'S METHOD


    [self.navigationController popViewControllerAnimated:YES];


as you can see, I want to call the setTitle() in BNRDetailViewController(delegate) from BNRAssetTypeViewController(child). This does not work…


Okay, I figured it out. After a long hard battle, it all came down to one line of code that made it work:

- (IBAction)showAssetTypePicker:(id)sender {

 avc.delegate = self;   //<----- HERE

This problem made me tired and restless. But after solving it, I felt like this:

:mrgreen: :mrgreen: :mrgreen: