繁体   English   中英

为什么在加载TableViewcontroller时出现此错误

[英]Why am I getting this error when loading my TableViewcontroller

我正在使用ARC和Storyboard开发iOS5项目。

我有一个带批注的mapview,其中的公开按钮转到我的DetailView(这是一个TableViewController),但是当应该加载它时,出现以下错误:

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) 

这是我的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

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

这是通过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

-----更多调试信息-------

使用异常断点时,它在这里停止:

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

安慰:

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)

似乎您的代码中的某处您正在尝试复制UILabel。 UIView子类不遵循NSCopying协议,因此尝试复制任何UIView都会导致异常(除非它是实现该协议的子类)。 我会在您的代码库中进行搜索,以检查复制发生的位置。 如果找不到任何明确复制UILabel的位置,请检查集合类。 某些集合类将复制值(NSDictionary使用键进行此操作,因此,如果您尝试将UILabel用作NSDictionary的键,则会收到错误)。 还要检查您的属性。 您在显示的代码中显示的内容很好,但是,如果您在UILabel的协议中指定了copy ,则也会出现此错误。

您还应该尝试标准的调试技术,例如为所有异常设置断点。 (转到断点导航窗格,在底部单击加号)。 这应该可以通过中断令人讨厌的代码行来帮助您找到问题。 请注意,如果复制是以Apple的代码进行的(例如设置NSDictionary密钥),则断点可能不会在您的代码上中断。

更新

给定您的新代码,我建议尝试以下操作:

  1. 尝试在代码中找到复制“任何内容”的任何位置。 显然,您没有显式复制UILabel,但可能是由于在某个地方传递了错误的参数而意外地复制了它。 例如:从事件属性中删除所有copy关键字,然后再次进行测试。 无论如何,您可能都不需要为NSString指定copy ,但是我们在这里要做的是确保您不会意外地将UILabel设置为这些属性之一。 如果代码没有中断,则您没有修复它,但是现在您知道了问题所在(尝试将UILabel分配给错误的属性)。
  2. 开始注释掉各种代码块,一次注释一次,以发现问题。 例如,注释掉整个viewDidLoad方法并测试代码。 如果有效,则说明问题出在那儿。 因此取消注释该方法的一部分并再次测试。 不断重复,直到您缩小问题的范围为止。 如果仍然无法通过viewDidLoad中断,请取消注释并尝试其他代码块。 本质上,您是在扮演侦探,试图找到令人讨厌的代码行。

解决此问题的方法是重命名UIViewControllertitle属性。 这是由于UIViewController已经定义了一个名称为titleNSString *属性。

这通常可能意味着您试图将对象分配给无法处理的方法,而不是该对象的属性。 例如,尝试将UILabel设置为对象,而不是该对象的字符串属性。

我的猜测是这行: [gemeente setText:[event gemeente]];

这里也可能存在问题:

@property (nonatomic) Event *event;

您尚未将Event *event设置为实例变量,但将其作为一个实例引用。 尝试使用self.event访问事件,因为上面有它。

暂无
暂无

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

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