2013-08-02 57 views
0

有了這段代碼每次用戶點擊任何單元格都會一次又一次地執行segue,我想知道如何跟蹤加載的視圖以便在切換視圖時保留數據而不是無限的新viewcontroler。performSegueWithIdentifier on didSelectRowAtIndexPath

謝謝 -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == 0) { 
    [self.navigationController 
    performSegueWithIdentifier:@"rep" sender:self]; 
    } else if (indexPath.row == 1) { 
    [self.navigationController 
    performSegueWithIdentifier:@"rep1" sender:self]; 
    } 

} 

回答

0

首先嚐試我的其他方法,但如果您確實需要維護指向新視圖控制器的指針,則可以嘗試使用此方法。這應該執行一次segue,創建對視圖控制器的引用,然後將其手動推入導航控制器。

重寫視圖控制器方法:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{ 

    if(self.myViewController == nil){ 
      return YES; 
    }else{ 
      [self.navigationController pushViewController:self.myViewController animated:YES] 
    } 
} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 

    self.myViewController = (MyViewController*)segue.destinationViewController; 
    self.myViewController.customVar = 1; //perform initial customization 

} 

什麼我雖然知道,我從來沒有用故事板...

-2

我認爲你可以保持新的viewController的指針。然後下次你可以這樣使用它:

[self.navigationController pushViewController:thePointer animated:YES] 

我很少使用Storyboard。所以我不確定它會起作用。

0

也許維護對視圖控制器的引用的另一種方法是在seque之前自定義視圖控制器。

重寫視圖控制器方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 

    MyViewController *targetController = (MyViewController*)segue.destinationViewController; 
    targetController.customVar = 1; 

} 

此方法的默認實現不執行任何操作。當需要將相關數據傳遞到新視圖控制器時,您的視圖控制器將覆蓋此方法。 segue對象描述了轉換,幷包含對segue中涉及的兩個視圖控制器的引用。

0

除非您想要自定義,否則您不必每次都爲每一行都帶上新的View控制器。這將在故事板上製作大量的視圖控制器。例如:如果A是tableViewController,而B是簡單的VC,你在顯示錶行的數據,那麼命令從整個tableViewController拖到B上。現在,這將作爲普通只有一個標識符。

因此,在你的代碼:

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

//Set your B's label/property etc to cell's data or anything so that it will reflect in B. 
B.label = cell.text; 
[self performSegueWithIdentifier:@"Identifier" sender:self]; 

} 

希望這有助於。

相關問題