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!