简体   繁体   English

收到“选择器的未知实例方法”错误,我不知道为什么

[英]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. 我正在尝试使用segue将tableevent中的'eventId'值传递给另一个视图控制器。

TableViewController.m 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];" “ [[segue destinationViewController] setEventId:id];” is giving the error of "No Known instance method for selector 'setEventId'" 给出错误“选择器'setEventId'的未知实例方法”

Below is "DetailsViewController.h" and "DetailsViewController.m" for the view I am attempting to segue to. 下面是我尝试选择的视图的“ DetailsViewController.h”和“ DetailsViewController.m”。

DetailsViewController.h DetailsViewController.h

#import <UIKit/UIKit.h>

@interface DetailsViewController : UIViewController <UISplitViewControllerDelegate>

@property (copy,nonatomic) NSString *eventId;

@end

DetailsViewController.m 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. 我希望至少有人可以提示该错误的原因。

Educated guess: You did forgot to #import DetailsViewController.h in TableViewController. 有根据的猜测:您确实忘记了在TableViewController中#import DetailsViewController.h
Xcode got a bit picky about those messages to id lately. Xcode最近对id那些消息有些挑剔。 It now complains if it does not know such a method at all. 现在,它抱怨根本不知道这种方法。 So #import the file. 因此,#import文件。

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. 但是,如果您#import正确的文件,它应该也可以正常工作,因为[segue destinationViewController]返回id。


When I said id here I meant id, not your variable that is named id. 当我在这里说id时,我指的是id,而不是您的名为id的变量。

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. 还要确保您导入DetailsViewController标头。

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. 如果在运行应用程序时发生错误,可能是因为您的Segue没有返回DetailsViewController实例,所以您应仔细检查情节提要,以确保使用该标识符为视图控制器提供了正确的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM