2014-07-21 57 views
0

我有一個tableview控制器,我想重寫最後一行的segue。我不想去標準的目標視圖控制器,而是想將它發送到另一個控制器。我該怎麼做呢?iOS重寫一個Segue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    NSIndexPath *myIndexPath = [self.tableView 
          indexPathForSelectedRow]; 
    long row = [myIndexPath row]; 
    if ([[segue identifier] isEqualToString:@"ShowLocationsTableView"]) 
    { 
     LocationsTableViewController *ViewController = 
     [segue destinationViewController]; 

     ViewController.categoryDetailModel = @[_categoryTitle[row], 
              _categoryImages[row]]; 
    } 
} 

這是當前的prepareForSegue,我想改變它。我想使用segue「aboutCat」將它發送給另一個視圖控制器。我該怎麼做呢?

我不明白prepareForSegue [current]和viewDidLoad [destination]之間會發生什麼。由於

副作用

最初的問題是由「尼基塔接過」解決但有一些副作用

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow]; 
    long row = [myIndexPath row]; 
    if ([_categoryTitle[row] isEqualToString:@"About"]) 
    { 
     NSLog(@"SKIPPING PREPARE"); 
    } 
    else if ([[segue identifier] isEqualToString:@"ShowLocationsTableView"]) 
    { 
     LocationsTableViewController *ViewController = 
     [segue destinationViewController]; 

     ViewController.categoryDetailModel = @[_categoryTitle[row], 
              _categoryImages[row]]; 
    } 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (tableView == self.tableView) 
    { 
     // I assume you have 1 section 
     NSLog(@"DIDSELECTROW"); 
     NSLog(@"INDEX PATH %i", indexPath.row + 1); 
     NSLog(@"%i", [tableView numberOfRowsInSection:0]); 
     if (indexPath.row + 1 == [tableView numberOfRowsInSection:0]) 
     { 
      NSLog(@"ABOUT"); 
      [self performSegueWithIdentifier:@"aboutCat" sender:self]; 
     } 
     else 
     { 
      NSLog(@"ELSE"); 
      [self performSegueWithIdentifier:@"ShowLocationsTableView" sender:self]; 
     } 
    } 
} 

現在大約作品!但是如果我點擊另一行,這是主要的一個。它會調用子視圖控制器2x。我可以使用詳細視圖viewdidload中的NSLOG語句驗證此情況。問題是現在導航控制器需要兩個步驟才能返回家中。

[首頁] - > [詳細視圖] - > [相同的細節視圖]

我可以驗證didSelectRowAtIndexPath方法的prepareforsegue

爲什麼是兩個塞格斯是預製任何想法之前發生?

+0

然後在didselctrowatIndexpath –

+0

添加另一個SEGUE我可以再補充該方法?任何其他行動需要? – Marcus

+0

你需要一些不同的方法 –

回答

2

假設你有兩個塞格斯:MainSegue(爲除所有行最後一個)和LastRowSegue(最後一行),你可以這樣做:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (tableView == self.tableView) { 
     // I assume you have 1 section 
     if (indexPath.row == [tableView numberOfRowsInSection:0]) { 
      [self performSegueWithIdentifier:@"LastRowSegue" sender:self]; 
     } else { 
      [self performSegueWithIdentifier:@"MainSegue" sender:indexPath]; 
     } 
    } 
} 

嘗試此prepareForSegue如果它不是你所需要的,請解釋一下你的意思是

它的工作原理,但我。我得到了雙重身份ayered表視圖

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"MainSegue"]) { 
     NSIndexPath *selectedIndexPath = sender; 
     LocationsTableViewController *locationController = segue.destinationViewController; 
     locationController..=categoryDetailModel = @[_categoryTitle[selectedIndexPath.row], _categoryImages[selectedIndexPath.row]]; 
    } 
} 
+0

它的工作原理,但我得到雙層表意見。我應該刪除prepareforsegue方法嗎? – Marcus

+0

是的,您不應該在'prepareForSegue'中更改segue標識符或segue目的地 –

+0

我在哪裏放置此代碼(來自prepareforsegue)?:這是爲MainSegue,但我如何傳遞這些值 - >'LocationsTableViewController * ViewController = [segue destinationViewController]; ViewController。categoryDe​​tailModel = @ [_ categoryTitle [row], _categoryImages [row]];' – Marcus

1

您無法在prepareForSegue中執行此操作 - 源和目標視圖控制器已在prepareForSegue調用時定義。你需要從你的控制器(未小區)進行兩次塞格斯和呼叫performSegueWithIdentifier:發件人」在didSelectRowAtIndexPath方法選擇其中Segue公司基礎上,indexPath執行

0

你可以做這樣的事情

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

int lastIndex=[tableArray count]-1; 
     if (indexPath.row == lastIndex) { 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
           NSString* viewType = @"MenuVC";//u can have the identifier of the LocationViewCOntroller or where ever you wanna go from here 
           UIViewController* viewController = [storyboard instantiateViewControllerWithIdentifier:viewType]; 
           self.navigationController.viewControllers = [NSArray arrayWithObjects:viewController, nil]; 
     } else { 

      [self performSegueWithIdentifier:@"usualSegue" sender:self]; 

     } 
}