C.Johns C.Johns - 1 year ago 75
iOS Question

Unrecognized selector, Delegate problems

I am getting this error

[ResultViewController setSearchFields:IndexPath:]: unrecognized selector sent to instance 0xc448840

I have a navigation controller stack in which I am using delegates to pass information back to a previous view in the navigation stack.. However I think I am doing something wrong when I am declaring the delegate.

My navigation stack looks like this.

view 0 (mainmenu)
-- view 1 (SearchViewController)
--- view 2 (ResultViewController) - where I set the delegate of the new view being loaded
---- View 3 (SubViewController) - this is where my delegates reside

What I am doing is popping to view1 and passing the delegate information to that view however by doing so I am getting this error... I am wondering if I have to set the delegate for view 3 in view 1 where I end up passing the information... is that correct??

If so what do I have to think about when setting the delegate? How do I call it from view 1

This is how I'm setting up my delegate in SubViewController


@protocol PassSubSearchData <NSObject>
- (void) setSearchFields:(NSArray *)modArray IndexPath:(NSIndexPath *)modIndexPath;

@interface VehicleSubResultViewController : UITableViewController <NSXMLParserDelegate> {
//Delegate for passing Mod and SubMod data back to VehicleSearchViewController
id <PassSubSearchData> delegate;
//Delegate for passing Mod and SubMod data back to VehicleSearchViewController
@property (strong) id delegate;


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//Access selected cells content (cell.textLabel.text)
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

//Predicates restrict the values that will be returned from the query.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"SUB",cell.textLabel.text];
filterArray = [parsedDataArrayOfDictionaries filteredArrayUsingPredicate:predicate];

[[self delegate] setSearchFields:tempModArray IndexPath:tempModIndexPath];

//This pops to the View 1 - SearchViewController
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];


Then In my SearchViewController this is how I am setting the delegate stuff up


#import "SubViewController.h"

@interface SearchViewController : UITableViewController <PassSubSearchData> {


- (void) setSearchFields:(NSArray *)modArray IndexPath:(NSIndexPath *)modIndexPath
modSearchObjectString = [[modArray valueForKey:@"MOD"] objectAtIndex:0];
modSearchIndexPath = modIndexPath; // Sets the selected IndexPath from the subview

NSLog(@"%@", modSearchObjectString);
NSLog(@"%@", modResultIndexPath);

[self.tableView reloadData];

That pretty much sums it up.. sorry for the delay.

Answer Source

So from what I can tell, storing the information into a file and then bringing it back down when it is needed might be a simpler way to achieve what you want. Something (I know, oversimplified) like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //store the cell's title into a string
    NSString* string = [[[tableView cellForRowAtIndexPath:indexPath] textLabel] text];

    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"someKey"];

Later you can get that string back by using:

NSString* titleString = [[NSUserDefaults standardUserDefaults] objectForKey:@"someKey"];

You can proceed in this same way with the information that you need to transfer.

Another alternative is to create a property in the view controller receiving the data:

@property(nonatomic, retain) NSString* string; //you need to synthesize it in the .m file too

then before you pop to the view controller you do:

//make sure you cast it
(SearchViewController*)[self.navigationController.viewControllers objectAtIndex:1].string = @"some string";

then once you go to that controller, that property will be set with whatever string you set it to in

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

And in these ways you can pass information between view controllers. Good luck!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download