2014-01-29 54 views
1

我的問題與this one類似,但我感覺不同,所以我需要在這裏提出問題。iOS - 在'NSArray'類型的對象上找不到屬性「name」

我正在創建我的第一個iOS應用程序,一個基本的主 - 細節應用程序,並使用JSONModel來提取API,但我認爲在這種情況下並不多。我有一個UITableView,其中每一行代表一個拳擊手,每個拳擊手都有一個名稱,重量級別,記錄等,這一切都很好。我遇到問題的地方是在各個詳細信息頁面上顯示該數據。主要是我無法弄清楚我應該如何在segue中傳遞數據,我知道我想使用該行來傳遞關聯的boxer對象,但似乎無法找出正確的方法。在我的代碼中,我有NSArray,但也嘗試過NSMutableArray和NSObject,每個都有相同的錯誤:在'NSW whatever'類型的對象上找不到屬性'name'。任何幫助,將不勝感激。謝謝。

BoxerMasterViewController.m

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
Boxer* boxerDetails = _feed.boxers[indexPath.row]; 

BoxerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BoxerCell" forIndexPath:indexPath]; 
cell.boxerNameLabel.text = [NSString stringWithFormat:@"%@", boxerDetails.name]; 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
if ([[segue identifier] isEqualToString:@"showBoxerDetail"]) { 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    NSArray *object = _feed.boxers[indexPath.row]; 
    [[segue destinationViewController] setBoxerDetailItem:object]; 

} 
} 

BoxerDetailViewController.h

#import <UIKit/UIKit.h> 
@interface BoxerDetailViewController : UIViewController 
@property (strong, nonatomic) NSArray *boxerDetailItem; 
@property (weak, nonatomic) IBOutlet UILabel *boxerName; 
@end 

BoxerDetailViewController.m

self.boxerName.text = [self.boxerDetailItem.name]; 

回答

4

Boxer類型的對象上使用boxerDetails.name,那是你想要的類型boxerDetailItem

@property (strong, nonatomic) Boxer *boxerDetailItem; 

使用此行的代碼不同的兩種情況:

Boxer* boxerDetails = _feed.boxers[indexPath.row]; 

NSArray *object = _feed.boxers[indexPath.row]; 

的對象是一個義和拳或一個NSArray。你決定哪個。 (我的猜測是義和團)

這是我對你的代碼的解釋:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
Boxer* boxerDetails = _feed.boxers[indexPath.row]; 

BoxerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BoxerCell" forIndexPath:indexPath]; 
cell.boxerNameLabel.text = [NSString stringWithFormat:@"%@", boxerDetails.name]; 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
if ([[segue identifier] isEqualToString:@"showBoxerDetail"]) { 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    Boxer *object = _feed.boxers[indexPath.row]; 
    [[segue destinationViewController] setBoxerDetailItem:object]; 

} 
} 

@interface BoxerDetailViewController : UIViewController 
@property (strong, nonatomic) Boxer *boxerDetailItem; 
@property (weak, nonatomic) IBOutlet UILabel *boxerName; 
@end 
+0

eugh,你是對的,那會教我跑遍百家教程,看起來每個代碼都需要一些代碼。我要解決我的問題,如果沒有錯誤將其標記爲正確的答案。謝謝! –

+0

這是你沒有列出的代碼的一部分,所以我沒有建議你在那裏。 – Putz1103

1

你想「boxerDetailItem」的類型爲「義和團」的,因爲你是在一個單一的「義和團」對象傳遞不「義和團」對象的「NSArray」。

相關問題