2011-06-16 62 views
0

設置:我有一個自定義類,我創建了一個使用ASIHttpRequest處理從Web源檢索的數據。這個數據是json編碼的。在異步請求的完成塊中,將自定義對象分配給內存,並將json值設置爲其相應的屬性。 此物件屬於的視圖。在完成塊中,調用一個設置視圖的方法,並將對象傳遞給一個從UIView子類化的headerView,然後將其設置爲tableView.tableHeaderView。此時一切正常。 headerView中的UILabels是使用自定義對象屬性設置的,而不會出現問題。對象屬性在某些方法中拋出未知錯誤

問題:在視圖中,我有一個調用方法的UIBarButton。當這個方法被調用並且我嘗試訪問自定義對象中的任何屬性時,程序崩潰(iPhone模擬器凍結)並且xcode指向屬性試圖訪問的行並且顯示「線程1」。調試器中沒有任何錯誤。當我NSLog的自定義對象,有一個內存地址返回匹配的地址,當我沒有任何問題訪問它。

任何想法?他們的任何調試方法是否有助於解決這個問題?我不明白爲什麼一個視圖的方法訪問這些屬性時沒有任何問題,另一個會。謝謝。

編輯:我禁用了殭屍,現在我收到一個EXC_BAD_ACCESS錯誤,而不是完全沒有錯誤。

編輯:代碼

@class Obj, ObjHeader; 
@interface ObjTableViewController : UITableViewController <UIActionSheetDelegate> { 

@public 
    NSString* objID; 
@private 
    Obj* obj; 
    ObjHeader* headerView; 
} 
@property (nonatomic, retain) NSString* objID; 
@property (nonatomic, retain) Obj* obj; 

@property (nonatomic, retain) ObjHeader* headerView; 

- (void)loadObj; // Loads Obj via async request 
- (void)setupView; // This method runs after a async request is successful 

- (void)viewDidLoad 
{ 
    [self loadObj]; 
    [super viewDidLoad]; 
} 

- (void)loadObj { 
    // Start request 
    ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:OBJ_URL]]; 
    [request setPostValue:self.objID forKey:@"ObjID"]; 

    // Successful request 
    [request setCompletionBlock:^{ 
     self.obj = [[Obj alloc] initWithString:[request responseString]]; 
     [self setupView]; 
    }]; 

    [request setFailedBlock:^{ 
     NSError *error = [request error]; 
     NSLog(@"Error on obj request with error %@",[error localizedDescription]); 
    }]; 

    [request startAsynchronous]; 
} 

    - (void)setupView { 
     // Header setup 
     self.headerView = [[ObjHeader alloc] initWithObj:self.obj]; 
     self.tableView.tableHeaderView = self.headerView; 



     // Title 
     self.title = self.obj.name; // SUCCESSFUL ACCESS HERE 
     UIBarButtonItem* optsButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction 
                        target:self 
                        action:@selector(showOpts:)] 
             autorelease]; 

     self.navigationItem.rightBarButtonItem = optsButton; 

    } 
- (void)showOpts:(id)sender { 
    NSLog(@"%@",self.obj.name); // EXC_BAD_ACCES 
} 

的OBJ類

@interface Obj : NSObject { 

@public 
    NSString* name; 
} 

@property (nonatomic, readonly, retain) NSString* name; 
- (id)initWithString:(NSString*)responseString; 

- (id)initWithString:(NSString*)responseString { 
    self = [super init]; 
    if(self) { 
     NSArray* json = [[NSString stringWithString:responseString] JSONValue]; 
     name = [json valueForKeyPath:@"Obj.Name"]; 
    } // END IF 
    return self; 
} 

ObjHeader類 - 我敢肯定,這是無關緊要的,因爲當我不使用它的問題仍然存在! :P

@class Obj; 
@interface ObjHeader : UIView { 

@public 
    UILabel* nameLabel; 
} 
@property (nonatomic, readonly, retain) UILabel* nameLabel; 

- (id)initWithObj:(Obj*)obj; 


- (id)initWithObj:(Obj*)obj 
{ 
    CGRect frame = CGRectMake(0, 0, 320.0f, 480.0f/4.0); 
    self = [super initWithFrame:frame]; 
    if (self) { 

     const CGFloat labelWidth = 320.0f - 15.0f; 

     // Setup Name label 
     const CGFloat nameFontSize = 17.0f; 
     CGRect nameFrame = CGRectMake(70.0f, 10.0f, 
             labelWidth, nameFontSize); 
     nameLabel = [[UILabel alloc] initWithFrame:nameFrame]; 
     nameLabel.text = obj.name; 
     nameLabel.font = [UIFont fontWithName:SYSFONT size:nameFontSize]; 
     [self addSubview:nameLabel]; 
    } 
    return self; 
} 
+0

如果您可以將代碼發佈到您分配和分配您的自定義對象的位置,它將對我們有所幫助。 – highlycaffeinated 2011-06-16 14:22:14

+0

控制檯中的錯誤怎麼辦? – JeremyP 2011-06-16 14:32:55

+0

控制檯爲空。 – Trevor 2011-06-16 14:51:14

回答

1

我認爲這裏有一個問題:

name = [json valueForKeyPath:@"Obj.Name"]; 

所以你想要麼在開始self.name或在年底保留這是一個自動釋放的對象。

+0

謝謝你,ZOMBIE耶穌爲了這個答案! – Trevor 2011-06-16 15:02:39

+0

我沒有意識到我沒有用自己的東西。 – Trevor 2011-06-16 15:17:34

+0

這很容易做到。我幾十次犯了同樣的錯誤:) – JosephH 2011-06-16 16:14:27

0

這可能是一個無限循環的問題。檢查你是否有相同的方法名稱和實例var?就像如果你聲明一個名爲myvalue的一個實例var和創建一個名爲

- (void)setMyValue; 

檢查方法,如果是這種情況或無限while循環。

+0

此時我沒有使用任何循環。 – Trevor 2011-06-16 14:48:51

相關問題