简体   繁体   中英

Why am I getting this error when loading my TableViewcontroller

I'm working on a project for iOS5 using ARC and storyboard.

I have a mapview with annotations with a disclosure button going to my DetailView (which is a TableViewController) but when it's supposed to be loaded, I get the following error:

2012-07-18 14:09:43.328 Zone-It-new[1966:707] Loadded the view for MapViewController
2012-07-18 14:11:40.467 Zone-It-new[1966:707] -[UILabel copyWithZone:]: unrecognized selector sent to instance 0x138470
2012-07-18 14:11:40.470 Zone-It-new[1966:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel copyWithZone:]: unrecognized selector sent to instance 0x138470'
*** First throw call stack:
(0x3748488f 0x35189259 0x37487a9b 0x37486915 0x373e1650 0x311997ef 0x31195059 0x31194711 0x3119466b 0x311945e7 0x31284f63 0x311979bb 0x311973ad 0x31191b8b 0x311917d5 0x9386d 0x3120a93d 0x31284627 0x37cf5933 0x37458a33 0x37458699 0x3745726f 0x373da4a5 0x373da36d 0x33b99439 0x31186cd5 0x924b3 0x92458)
terminate called throwing an exception(lldb) 

This is my detailviewcontroller.h:

#import <UIKit/UIKit.h>
#import "Event.h"

@interface DetailViewController :  UITableViewController <UITableViewDelegate, UITableViewDataSource>{
     IBOutlet UILabel *title;
     IBOutlet UILabel *description;
     IBOutlet UILabel *zone;
     IBOutlet UILabel *gemeente;
     IBOutlet UILabel *plaats;
     IBOutlet UILabel *deelnemers;
     IBOutlet UILabel *start;
     IBOutlet UILabel *einde;
     IBOutlet UILabel *id_nr;
}

@property (nonatomic) Event *event;
@property (nonatomic) IBOutlet UILabel *title, *zone, *description, *gemeente, *deelnemers, *start, *einde, *plaats, *id_nr;
@end

part of the DetailViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[self navigationController] setNavigationBarHidden:NO animated:YES];


    /*title.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;*/
    [title lineBreakMode];
    [title setText:[event title]];
    [title sizeToFit];

    gemeente.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    [title lineBreakMode];
    [gemeente setText:[event gemeente]];
}

And this is where the view gets created in via the ListView:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
    SingletonManager *sharedManager = [SingletonManager sharedManager];

    [detail setEvent:[sharedManager.eventsManager objectAtIndex:indexPath.row]];

    [self.navigationController pushViewController:detail animated:YES];
}

Event.h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface Event : NSObject <MKAnnotation>


// required property from mkanotation
@property (nonatomic) CLLocationCoordinate2D coordinate;

// Optional
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *description;
@property (nonatomic, copy) NSString *zone;
@property (nonatomic, assign) NSString *gemeente;
@property (nonatomic, copy) NSString *straat;
@property (nonatomic, assign) int deelnemers;
@property (nonatomic, copy) NSDate *start;
@property (nonatomic, copy) NSDate *einde;
@property (nonatomic, copy) NSString *plaats;
@property (nonatomic, readonly) int id_nr;
@property (nonatomic, assign) int huisnummer;



@end

----- Some more debugging information -------

While using the exception breakpoints it stopped here:

[self.navigationController pushViewController:detail animated:YES];

console:

2012-07-18 15:53:46.691 Zone-It-new[179:707] Loadded the view for MapViewController 
2012-07-18 15:54:01.940 Zone-It-new[179:707] -[UILabel copyWithZone:]: unrecognized selector sent to instance 0x170bb0 
(lldb)

It appears that somewhere in your code you're trying to copy a UILabel. UIView subclasses don't adhere to the NSCopying protocol, so trying to copy any UIView will result in an exception (unless it's a subclass that does implements the protocol). I'd search through your code base to check where the copy is occurring. If you can't find any place that explicitly copies the UILabel, check collection classes. Some collections classes will copy values in (NSDictionary does this with keys, so if you're trying to use a UILabel as a key for a NSDictionary, you'll get the error). Also check your properties. What you show in your displayed code is fine, but if you specify a copy in a protocol for a UILabel, that'll also give the error.

You should also try standard debugging techniques like setting a breakpoint for all exceptions. (go to the breakpoints nav pane, at the bottom click the plus sign). This should help you find the problem by breaking on your offending line of code. Just be aware that if the copying is occurring in Apple's code (like setting a NSDictionary key) the breakpoint might not break on your code.

Update

Given your new code, I would suggest trying the following:

  1. Try to find any place in your code where "anything" is copied. Obviously your not explicitly copying UILabel but maybe you're accidentally copying it by passing the wrong argument somewhere. For example: Remove all copy keywords from your Event properties and test it again. You probably don't need to specify copy for NSString anyway, but what we're doing here is making sure you're not accidentally setting a UILabel to one of those properties. If the code doesn't break, you didn't fix it but now you know what the problem is (trying to assign a UILabel to the wrong property).
  2. Start commenting out various blocks of code, one at a time, to find the problem. For example, comment out your entire viewDidLoad method and test your code. If it works, you know the problem is in there. So uncomment part of the method and test again. Keep repeating until you've narrowed down the problem. If it still breaks with viewDidLoad , uncomment that and try a different block of code. In essence, you're playing detective to try to find the offending line of code.

The fix for this is to rename the title property in the UIViewController . This is due to the fact that UIViewController already defines an NSString * property with a name of title .

This can often mean you're trying to assign an object to a method that can't handle it, rather than a property of that object. For example, trying to set a UILabel to an object rather than a string property on that object.

My guess is it's this line: [gemeente setText:[event gemeente]];

There could also be an issue here:

@property (nonatomic) Event *event;

You haven't set Event *event as an instance variable but are referencing it as one. Try self.event for accessing event as you have it above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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