2012-02-23 134 views
0

我正在做一個應用程序,其中有一個UITableViewController填充產品列表。每一行都跳轉到一個UIViewController,它顯示了該行中產品的詳細信息。現在,由於在每一行上點擊並返回以查看下一個產品的細節對用戶來說可能太乏味,所以我們決定添加此功能:當用戶在產品的UIViewController上滑動時,然後使用UIViewController下一個產品被推送。授權和解僱視圖控制器

但是,截至目前,我不確定實現這一點的最佳方式。我試圖將產品數組傳遞給UIViewController,以便實現刷卡,但這會違反MVC框架,對吧?視圖不能擁有他們正在呈現的數據。產品詳細信息UIViewController應該只知道傳遞給它的特定產品,而不是剩下的,對吧?

我認爲這可以使用委託來完成,但我不知道如何。有誰能夠幫助我?謝謝!

編輯: Rob Mayoff的代碼真的很有用,所以我決定實現它。但是與此同時,我不會實施滑動操作,而只是使用簡單的圓形按鈕來調用函數。

- (IBAction)showNextProduct:(id)sender { 
    [self.productsTVC goToProductAtIndex:self.productIndex + 1]; 
} 

- (IBAction)showPriorProduct:(id)sender { 
    [self.productsTVC goToProductAtIndex:self.productIndex - 1]; 
} 

但每次我點擊任何按鈕,我與消息應用程序崩潰:Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted. Unbalanced calls to begin/end appearance transitions for <ProductDetailsViewController: 0x6e510c0>.

回答

3

比方說,你有一個CatalogViewController(這是UITableViewController一個子類)和ProductViewController(這是一個UIViewController的子類)。

實施「刷卡到下一個產品」的最簡單方法是給ProductViewController參考CatalogViewController。它應該是weak(如果使用ARC)或assign(如果不使用ARC)。您還需要保存的產品在目錄索引的屬性:

@interface ProductViewController 

@property (nonatomic, weak) CatalogViewController *catalogViewController; 
@property (nonatomic) NSInteger productIndex; 

@end 

然後在刷卡操作方法,您將消息發送到CatalogViewController要求它進入下一個(或前)產品在目錄:

@implementation ProductViewController 

- (IBAction)showNextProduct:(id)sender { 
    [self.catalogViewController goToProductAtIndex:self.productIndex + 1]; 
} 

- (IBAction)showPriorProduct:(id)sender { 
    [self.catalogViewController goToProductAtIndex:self.productIndex - 1]; 
} 

CatalogViewController,當你創建一個ProductViewController,你需要設置這些屬性:

@implementation CatalogViewController 

- (ProductViewController *)productViewControllerForProductAtIndex:(NSInteger)index { 
    if (index < 0 || index >= self.products.count) 
     return nil; 
    ProductViewController *vc = [[ProductViewController alloc] initWithProduct:[self.products objectAtIndex:index]]; 
    vc.catalogViewController = self; 
    vc.productIndex = index; 
    return vc; 
} 

你實現goToProductAtIndex:方法是這樣的:

- (void)goToProductAtIndex:(NSInteger)index { 
    ProductViewController *vc = [self productViewControllerForProductAtIndex:index]; 
    if (!vc) 
     return; 
    NSMutableArray *vcs = [[self.navigationController viewControllers] mutableCopy]; 
    while (vcs.lastObject != self) 
     [vcs removeLastObject]; 
    [vcs addObject:vc]; 
    [self.navigationController setViewControllers:vcs animated:YES]; 
} 

您可以用同樣的方法來處理表行選擇:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [self goToProductAtIndex:indexPath.row]; 
} 

如果你想獲得更多的軟件engineery,您可以創建一個圍繞着goToProductAtIndex:方法的協議,並用於避免使ProductViewController知道類CatalogViewController

+0

thanks @rob。我會嘗試這個解決方案,我會讓你知道。謝謝! – acecapades 2012-02-23 03:56:44

+0

rob,我收到了一條錯誤消息:「在意外狀態下完成導航轉換。導航欄子視圖樹可能會損壞。「和」不平衡調用開始/結束外觀轉換爲 acecapades 2012-02-23 06:16:07

+0

確保您所有的'viewWillAppear:','viewDidAppear:','viewWillDisappear:'和'viewDidDisappear:'方法調用他們的'super'方法 – 2012-02-23 06:20:00