2012-03-27 71 views
-2

如何將對象從uitableviews傳遞到我的詳細視圖?我已經設置了一個條件推,你可以在我的方法,請參閱:使用didSelectRowAtIndexPath傳遞對象:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    Budget * tempBudget = [self.budgetPlan.budgets objectAtIndex:indexPath.row]; 

    NSString *targetViewControllerIdentifier = nil; 
    if (tempBudget.hasBeenInitialized != true) { 
     targetViewControllerIdentifier = @"budgetInitializerView"; 
     UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:targetViewControllerIdentifier]; 
     [self.navigationController pushViewController:vc animated:YES]; 

     // how do I pass the tempBudget to the pushed view? 


    } else { 
     targetViewControllerIdentifier = @"budgetDetailsView"; 
     UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:targetViewControllerIdentifier]; 
     [self.navigationController pushViewController:vc animated:YES]; 

     // how do I pass the tempBudget to the pushed view? 

    } 
} 

我在與UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:targetViewControllerIdentifier];

麻煩通常情況下,我只設置 vc.budget = tempbudget的答案以下建議,但它似乎並不瞭解預算財產。

+0

燦你詳細說明「似乎不瞭解預算財產」?編譯器警告?崩潰?新視圖上沒有顯示任何內容? – 2012-03-27 18:09:50

+0

新視圖中不顯示任何內容。它似乎沒有通過。 – iggy2012 2012-03-27 20:52:30

回答

1

我會創建一個BaseViewController,該對象的類型爲@property,並以此方式發送對象。

// In your .h 
BaseViewController : UIViewController { 
} 

@property (nonatomic, strong) Budget *budget; 

//In your .m 
@synthesize budget; 

此外,您還可以將這一行你的if/else語句,因爲它是同樣的事情:

vc.budget = tempBudget; 
[self.navigationController pushViewController:vc animated:YES]; 

設定預算屬性你推右前視圖控制器

+0

如果我做vc.Budget = tempBudget,它似乎並沒有傳遞數據。 – iggy2012 2012-03-27 18:05:50

+0

另外,我不能移動vc.budget = tempBudget,因爲它是一個局部變量。 – iggy2012 2012-03-27 18:06:07

+0

如果你有一個在Class中創建的屬性,你肯定可以傳遞一個局部變量給它。 – 2012-03-27 18:19:34