user1987502 user1987502 - 3 months ago 11
Objective-C Question

Not able to access delegate method from presented view controller

Delegate declaration

// FolderListViewController.h
#import <UIKit/UIKit.h>
#import "Folder.h"

@protocol FolderSelectionDelegate <NSObject>

- (void)setFolder:(Folder *)folder;


@interface FolderListViewController : UITableViewController

@property (nonatomic, assign) id<FolderSelectionDelegate> delegate;

- (IBAction)showDashboard:(id)sender;


Delegate is called from didSelectRowAtIndexPath:

// FolderListViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.delegate setFolder:dataSource[indexPath.row]];

The VC that should be receiving the message

#import <UIKit/UIKit.h>
#import "FolderListViewController.h"
#import "Folder.h"

@interface ProjectListViewController : UITableViewController <FolderSelectionDelegate, UISplitViewControllerDelegate, UISearchBarDelegate, UINavigationControllerDelegate>

@property (nonatomic,retain)UIActivityIndicatorView *activityIndicatorObject;
@property (nonatomic, copy)Folder *folder;



Action that presents the VC

- (void)foldersButtonTapped {
UINavigationController *vc = [[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"FolderListNavController"];
vc.delegate = self;
[self presentViewController:vc animated:YES completion:nil];

Delegate method implementation

- (void)setFolder:(Folder *)folder {
_folder = folder;
[self loadProjects:folder];

I have read through multiple threads on here and haven't had any luck. At first, I didn't have the reference to ProjectListVC setup when presenting the FolderListVC (ie, vc.delegate = self). That doesn't seem to be the problem here though. I am working on an app that was built for iPad and scaling it to work across all devices. The implementation as it is here works (it's setup as a split view controller). Any help would be greatly appreciated


I think that UINavigationcontroller is creating problem as it is not setting the FolderListViewController delegate . Can You try like this.

 FolderListViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"FolderListViewController"];

    vc.delegate=self; // protocol listener
    [self.navigationController pushViewController:vc animated:YES];