'Delegate/Protocol doesn't work: pass Array from TableViewController to parent ViewController

I have a parent ViewController (SearchViewController) which segues to a TableViewController (FilterTableViewController) where the User can select persons in the TableView. The selections are stored in an NSMutableArray called selectedpersonArray, which works already fine. But now i need to pass them back to the parent ViewController. I tried to implement a protocol/delegate. Since I'm new to Objective-C I don't understand some parts properly and it doesn't work:

FilterTableViewController.h (Child Controller)

@protocol filterDelegate <NSObject>
-(void)addArraytoSearchViewController:(NSMutableArray *)array;
@end

@interface FilterTableViewController : UITableViewController

@property (nonatomic, assign) id <filterDelegate> selectedpersonDelegate;


@property (nonatomic, retain) NSMutableArray *selectedpersonArray;    

@end

FilterTableViewController.m

#import "FilterTableViewController.h"

@interface FilterTableViewController ()
@end

@implementation FilterTableViewController

...

@synthesize selectedpersonArray;
@synthesize selectedpersonDelegate;

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

[[self selectedpersonDelegate] addArraytoSearchViewController:selectedpersonArray];
NSLog(@"personarray: %@", selectedpersonDelegate);
}

Question 1: Can I show the delegate in NSLog? Here it returns nil, but selectedpersonArray contains values. Question 2: Is there a better place to pass the Array than didSelectRowAtIndexPath, because multiple selections are possible and the Array could be passed only when the Back button is pushed. Can I use *- (void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender?

SearchViewController.h: (Parent View)

#import "FilterTableViewController.h"

@interface SearchViewController: UIViewController <UITableViewDataSource, UITableViewDelegate, filterDelegate>

SearchViewController.m:

@implementation SearchViewController

...

- (void)viewDidLoad{
    FilterTableViewController * FTC = [[FilterTableViewController alloc] init];
    FTC.delegate = self;

- (void)addArraytoSearchViewController:(NSMutableArray *)array {
    NSLog(@"%@", array);
}

Question 3: NSLog doesn't do anything here... Whats wrong? Question 4: How can I pass more than one Array from the second ViewController?

thanks in advance!



Solution 1:[1]

i forgot to set the delegate as user firoze figured out. this solved my problem

Solution 2:[2]

It sounds as if you aren't actually setting the delegate on the FilterTableViewController that the segue is presenting for you. Just set the delegate in your prepareForSegue:sender: method where you are apparently pushing other data as well.

Right now in your -[SearchViewController viewDidLoad] method, you are creating some other instance called FTC, setting its delegate, and then promptly throwing that FTC instance away. That instance is unrelated to the one that will be pushed by the segue.

On your other questions:

1.) Yes, you should be able to see the delegate there.

2.) Yes, if you want to collect multiple selections, then it probably makes more sense to notify your delegate when the selections are complete.

3.) Likely this method is never called because you never set the delegate correctly.

4.) If you have two arrays, you might just include two parameters on your delegate method. If more than 2, I would think about a dictionary of arrays, or an array of arrays.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 halfer