0

I'm building an iPad app with storyboards. I have a "data page" (view) where I put one big container with a tableview which contains data. then I have 3 small containers above, also with tableviews, those should filter the data on the big container. In IB connected all tableviews with embed segues. Visually, it already builds exactly how I need to, all tables shown with content. so far so good.

now the communication part: for each "filter tableview" I implement a protocol method and delegate property, and conformed the "data" container tableview controller to it. But somehow the methods are not being called. I tried setting [self setDelegate:self] in the filter tableview controller, but that crashes the app with ": unrecognized selector sent to instance"

It's not my first delegate protocol, in my other (working) case I programmatically instantiate a view and needed to set the delegate to that view. How is it done here???

A following question would be: to conform my data container viewcontroller to 3 delegates, I just set 3 different type id properties? (meaning, calling them differently?)

Any hint hugely appreciated!

Edit: Code added. [filter:selection:] is not being called... no NSLogs..

//MainDataViewController.h

@interface MainDataViewController : UITableViewController <UITableViewDelegate,       UITableViewDataSource,FilterDelegate>

-(void)filter:(Object *)filterObject selecting:(BOOL)selection;

@end

//MainDataViewController.m

#import "MainDataViewController.h"
#import "FilterViewController.h"

@interface MainDataViewController ()

@end

@implementation MainDataViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
}

- (void)filter:(Object *)filterObject selecting:(BOOL)selection;
{
    NSLog(@"test");
    NSLog(@"selection object: %@", filterObject);

}

@end

//FilterViewController.h

@protocol FilterDelegate <NSObject>

- (void)filter:(Object *)filterObject selecting:(BOOL)selection;

@end

@interface FilterViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) id <FilterDelegate> delegate;

@end

//FilterViewController.m

#import "FilterViewController.h"

@interface FilterViewController ()

@end

@implementation FilterViewController

@synthesize delegate;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setDelegate:self];

    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
}
[self.delegate filter:object selecting:YES];
}
L00ps
  • 113
  • 2
  • 9
  • Not sure to understand your issue, but you can set your ViewController to be the delegate for each filters. Then in the `tableView:didSelectRowAtIndexPath`, you can check with an if statement which tableView called the method (by comparing tags for instance). – Alexis C. May 15 '13 at 13:41
  • Thanks a lot for your answer! But: isn't it the other way around? I need to pass the index of my 3 FilterTableViews TO the MainDataTableView. I thought the 3 need to be set as delegates to the MainView. Am I missing something? – L00ps May 15 '13 at 13:46
  • You should have a look at the delegation pattern. The delegate is a pointer you give to your filters for them to call their methods (declared in their protocol) onto. – Alexis C. May 15 '13 at 13:49
  • Ok. I already implemented another functioning delegate protocol, I may have misunderstood you. Did you mean to set each filters tableviewController delegate property to self? This is how I understand it from my other implementation. – L00ps May 15 '13 at 13:59
  • yep, that's it. This way it's always your viewController being called. – Alexis C. May 15 '13 at 14:06
  • ok thx, exactly what I have, but my app crashes. I did [self setDelegate:self] in viewdDidLoad of the filterVC and call in didSelectRow of the mainVC [self.delegate myMethod: selecting:YES]; throws: "-[FilterViewController myMethod:selecting:]: unrecognized selector sent to instance" – L00ps May 15 '13 at 14:16
  • Weird, if your method name is correct it should not raise any error. Maybe you could upload you ViewController code so we can have a look? – Alexis C. May 15 '13 at 14:58
  • good to know my understanding is correct. code added. – L00ps May 15 '13 at 15:45

2 Answers2

0

The [self setDelegate:self] does not look right. I would think it should look something more like:

[self.filter1TableView setDelegate:self];
[self.filter2TableView setDelegate:self];
[self.filter3TableView setDelegate:self]; 
bobnoble
  • 5,794
  • 3
  • 25
  • 32
  • Thanks a lot! Not sure I understand correctly. Do I need to instantiate my view first? Or set a property to the class? Since I use storyboard I don't have such a reference. – L00ps May 15 '13 at 14:32
  • In the storyboard, you want to create a reference to each of the filter table views (right click drag to the view controller .h file, giving each a name. (also note I added `self.` before each of the filter table view names) – bobnoble May 15 '13 at 14:47
0

I ended up removing the containerViews and presenting popovers. Delegate protocol working as expected. Very curious what I did wrong though.. I guess it has something to do with the parentView of the embedded views..?

L00ps
  • 113
  • 2
  • 9