簡體   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