iOS覆盖了Segue

时间:2014-07-21 06:01:20

标签: ios objective-c xcode segue

我有一个tableview控制器,我想覆盖最后一行的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]之间会发生什么。感谢

副作用

最初的问题由'Nikita Took'解决,但有一些副作用

-(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。我可以使用detailview viewdidload中的NSLOG语句来验证这一点。问题是现在导航控制器需要两个步骤才能回家。

[home] - > [详细视图] - > [相同的详细视图]

我可以验证didselectrowatindexpath发生在prepareforsegue之前

为什么要进行两个分段的任何想法?

3 个答案:

答案 0 :(得分:2)

假设你有两个segues: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。如果不是您需要的,请说明您的意思

  

它可以工作,但我正在获得双层表视图

-(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]];
    }
}

答案 1 :(得分:1)

您不能在prepareForSegue中执行此操作 - 源和目标视图控制器已在prepareForSegue被调用时定义。您需要从控制器(而不是单元格)中创建两个segues,并在didSelectRowAtIndexPath中调用performSegueWithIdentifier:sender“。根据indexPath选择要执行的segue。

答案 2 :(得分: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];

        }
}