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


#1

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).

BNRDetailViewController.h

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

BNRDetailViewController.m

[code]…
//*ADDED
#import “BNRAssetTypeViewController.h”

//*ADDED
@property (strong, nonatomic) UIPopoverController *itemTypePickerPopover;

@end

@implementation BNRDetailViewController

//ADDED
-(void)setTitle:(NSString*)title{

NSLog(@"XXXXXXXXXXsetTitle");

self.assetTypeButton.title = title;

}

//*ADDED

  • (IBAction)showAssetTypePicker:(id)sender {

    [self.view endEditing:YES];

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

    avc.item = self.item;

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

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

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

    }
    else{

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

    }

    //----------------------------->
    }

[/code]

and Here is the header and implementation for BNRAssetTypeViewController:

BNRAssetTypeViewController.h


#import <Foundation/Foundation.h>

@class BNRItem;

//*ADDED
@protocol BNRDetailViewController <NSObject>

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

@end

@interface BNRAssetTypeViewController : UITableViewController

@property (nonatomic, strong)BNRItem *item;


//*ADDED
@property (nonatomic,weak) id delegate;

@end

and the implementation file:

BNRAssetTypeViewController.m

...
//*ADDED
#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;
    
    //*ADDED

     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…


#2

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: