Jebzaki
Jebzaki

Reputation: 163

Getting a"No Known instance method for selector" error and I have no idea why

I'm trying to pass the 'eventId' value from my tableview to another view controller using the segue.

TableViewController.m

#import "TableViewController.h"
#import "EventCell.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(NSArray *)content

{
    if (!_content) {
        _content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
    }

    return _content;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // Return the number of rows in the section.
    return [self.content count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    EventCell *cell = (EventCell *)[tableView dequeueReusableCellWithIdentifier:@"EventCell"];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EventCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.eventImage.image = [UIImage imageNamed: [[self.content objectAtIndex:indexPath.row]valueForKey:@"picturename" ] ];

    cell.eventName.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"name"];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //return height of cell
    return 155;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self performSegueWithIdentifier:@"EventDetails" sender:self];
}

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"EventDetails"]){

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSString *id = [[self.content objectAtIndex:indexPath.row] objectForKey:@"id"];

        [[segue destinationViewController] setEventId:id];
        //[segue destinationViewController];
    }
}

"[[segue destinationViewController] setEventId:id];" is giving the error of "No Known instance method for selector 'setEventId'"

Below is "DetailsViewController.h" and "DetailsViewController.m" for the view I am attempting to segue to.

DetailsViewController.h

#import <UIKit/UIKit.h>

@interface DetailsViewController : UIViewController <UISplitViewControllerDelegate>

@property (copy,nonatomic) NSString *eventId;

@end

DetailsViewController.m

#import "DetailsViewController.h"

@interface DetailsViewController ()

@end

@implementation DetailsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I hope someone can at least give a hint to what's going on with that error.

Upvotes: 1

Views: 2061

Answers (2)

Matthias Bauch
Matthias Bauch

Reputation: 90127

Educated guess: You did forgot to #import DetailsViewController.h in TableViewController.
Xcode got a bit picky about those messages to id lately. It now complains if it does not know such a method at all. So #import the file.

I would make it explicit though:

DetailsViewController *detailsViewController = [segue destinationViewController];
detailsViewController.eventId = @"your id";

But if you #import the correct file it should work as well, because [segue destinationViewController] returns id.


When I said id here I meant id, not your variable that is named id.

Upvotes: 5

Gavin
Gavin

Reputation: 8200

If it is the compiler giving you the error, you should be able to do the following:

DetailsViewController *detailsViewController = segue.destinationViewController;
[detailsViewController setEventId:id];

Also make sure you import the DetailsViewController header.

If the error is happening while running the application, it may be because your segue isn't returning a DetailsViewController instance, so you should double-check your storyboard to make sure the correct type is given to the view controller with that identifier.

Upvotes: 2

Related Questions