Passing an Object between Table View Controller and it's Detail View Controller, getting NSInvalidArgumentException error

StackOverflow https://stackoverflow.com/questions/23318937

Question

I have been trying to solve this problem for quite some time now. I am having some difficulty passing a "Container" Object from a Table View Controller called ListTableViewController to its Detail View called "ListViewController" (I know I should probably change its name). Everytime I try to access the Container.name attribute of the Container object I get a NSInvalidArgument Errors and I don't know why, I am not even sure if the object is being passed properly although it seems like it should. Any Help would be greatly appreciated.

I am trying to access currentContainer in ListViewController

This is the code for my segue in ListTableViewController.m, which seems to work well: The NSLog in this block gives me the expected Container name which is "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);
}

My ListViewController.h has

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

@interface ListViewController : UIViewController

@property (nonatomic) Container *currentContainer;

@end

And my ListViewConroller.m has

#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];
}

This is the NSLOG values I have and stack trace:

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:
Was it helpful?

Solution

It looks like you must have set up currentContainer as a IBOutlet at some point (but you're not doing that now), because in the storyboard, I see currentContainer connected as an outlet to the same label that containerName is hooked up to. Delete that connection, and I think it should work correctly.

OTHER TIPS

Unless you have implemented a category or informal protocol that extends UILabel, UIView, UIResponder, or NSObject, UILabel does not respond to the selector name.

self.currentContainer is returning a UILabel, not an instance of your Container class (which would, I assume, respond to name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top