在Table View Controller和它的Detail View Controller之间传递一个对象,得到NSInvalidArgumentException错误

时间:2014-04-27 03:09:16

标签: ios objective-c unrecognized-selector

我一直试图解决这个问题很长一段时间了。我在通过" Container"时遇到了一些困难。表视图控制器中的对象名为ListTableViewController,其详细信息视图名为" ListViewController" (我知道我应该更改它的名字)。每次我尝试访问Container对象的Container.name属性时,我都会得到一个NSInvalidArgument错误而且我不知道为什么,我甚至不确定该对象是否正常传递,尽管看起来应该如此。非常感谢任何帮助。

我正在尝试访问ListViewController中的currentContainer

这是我在ListTableViewController.m中的segue代码,它似乎运行良好: 此块中的NSLog为我提供了预期的容器名称,即" salad"。

#import "ListTableViewController.h"
#import <RestKit/RestKit.h>
#import "Container.h"

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
    ListViewController *pvc = [segue destinationViewController];
    // Pass the selected object to the new view controller.
    //What is the selected Cell?
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    Container *c = _containers[path.row];
    pvc.currentContainer = c;
    NSLog(@"%@",c.name);
}

我的ListViewController.h有

#import <UIKit/UIKit.h>
#import "Container.h"
#import "ListTableViewController.h"

@interface ListViewController : UIViewController

@property (nonatomic) Container *currentContainer;

@end

我的ListViewConroller.m有

#import "Container.h"
#import "ListViewController.h"
#import "ListTableViewController.h"


@interface ListViewController ()

@property (weak, nonatomic) IBOutlet UILabel *containerName;

@end


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //UILabel *container = [UILabel currentContainer name];
    NSLog(@"%@",_currentContainer);
    NSLog(@"%@",_containerName);
    //self.containerName.text = @"test";
    //self.title = _currentContainer.name;
    self.containerName.text = [self.currentContainer name];
}

这是我拥有的NSLOG值和堆栈跟踪:

2014-04-26 22:48:26.452 freshlids[58546:60b] Salad
2014-04-26 22:48:26.455 freshlids[58546:60b] <UILabel: 0x986a640; frame = (20 143; 280 30); text = 'Label'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM;     userInteractionEnabled = NO; layer = <CALayer: 0x9869c20>>
2014-04-26 22:48:26.455 freshlids[58546:60b] <UILabel: 0x986a640; frame = (20 143; 280 30); text = 'Label'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM;     userInteractionEnabled = NO; layer = <CALayer: 0x9869c20>>
2014-04-26 22:48:26.456 freshlids[58546:60b] -[UILabel name]: unrecognized selector sent to instance 0x986a640
2014-04-26 22:48:26.459 freshlids[58546:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel name]: unrecognized selector sent to     instance 0x986a640'
*** First throw call stack:

2 个答案:

答案 0 :(得分:1)

看起来你必须在某个时候将currentContainer设置为IBOutlet(但你现在还没有这样做),因为在故事板中,我看到currentContainer连接为与containerName相同的标签的出口迷上了。删除该连接,我认为它应该正常工作。

答案 1 :(得分:-1)

除非您实施了扩展UILabelUIViewUIResponderNSObject的类别或非正式协议,否则UILabel不响应选择器name

self.currentContainer正在返回UILabel,而不是Container类的实例(我认为这会回复name