6

I'm invoking push segue programmatically as below

    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"addToCartViewContollerForItem"];
    [self.navigationController pushViewController: vc animated:YES];

but my problem is I want to send some info with this segue to the destination viewcontroller. But the method - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender is not getting fired at all.

this is how the prepareforsegue method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    NSLog(@"segue from deals screen");
    //addToCartViewContollerForItem
    if([[segue identifier] isEqualToString:@"addToCartSegue"]){
        NSIndexPath *selectedRow = [[self tableView] indexPathForSelectedRow];

        Item *currentItem = [[Item alloc]init];
        currentItem = [itemList objectAtIndex:[selectedRow row]];

        RESTAddToCartViewController *vc = [segue destinationViewController];
        [vc setCurrentItem:currentItem];
    }
}

What am I missing here?

5
  • On which action you want the segue to happen, like unbutton action or uitableview cell selection? Dec 13, 2013 at 5:23
  • uitableview cell selection. I did it using didSelectRowAtIndexPath
    – Jaya Mayu
    Dec 13, 2013 at 5:23
  • You can directly use prepareForSegue method without didSelectRow method with the code you written or you can call perform segue from your didSelectRow method, check my answer. Dec 13, 2013 at 5:30
  • I think the main confusion is in the first line of the post. That navigation controller push does not perform a push segue. self performSegueWithIdentitifer is the way to do that, as @suhit suggests.
    – danh
    Dec 13, 2013 at 5:31
  • @dahn rightly said, UINavigationController pushViewController does not perform the push segue, best way is performSegueWithIdentitifer and prepareForSegue: method. Dec 13, 2013 at 5:37

2 Answers 2

21

Call performSegueWithIdentifier: from your didSelectRowAtIndexPath method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"addToCartSegue" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    NSLog(@"segue from deals screen");
    //addToCartViewContollerForItem
    if([[segue identifier] isEqualToString:@"addToCartSegue"]){
        NSIndexPath *selectedRow = [[self tableView] indexPathForSelectedRow];

        Item *currentItem = [[Item alloc]init];
        currentItem = [itemList objectAtIndex:[selectedRow row]];

        RESTAddToCartViewController *vc = [segue destinationViewController];
        [vc setCurrentItem:currentItem];
    }

}

in Swift 2.3

//MARK: didSelectRowAtIndexPath

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("addToCartSegue", sender: self)
}


// MARK: - Navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    if segue.identifier == "addToCartSegue" {
        let selectedIndexPath = self.tableView.indexPathForSelectedRow
        let currentItem: Item = itemList[selectedIndexPath.row]
        if let viewcontroller = segue.destinationViewController as? RESTAddToCartViewController {
            viewcontroller.currentItem = currentItem
        }
    }
}

Swift 3

//MARK: didSelectRowAtIndexPath

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "addToCartSegue", sender: self)
}

// MARK: - Navigation

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "addToCartSegue" {
        if let selectedIndexPath = self.tableView.indexPathForSelectedRow {
            let currentItem: Item = itemList[selectedIndexPath.row]
            if let viewcontroller = segue.destination as? RESTAddToCartViewController {
                viewcontroller.currentItem = currentItem
            }
        }
    }
}
1
0

This is the information that you are looking for :

 (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        RecipeDetailViewController *destViewController = segue.destinationViewController;
        destViewController.recipeName = [recipes objectAtIndex:indexPath.row];
    }
}

prepareForSegue will get called along with [self perform segue] and this is the tutorial you should definitely look into.

2
  • 1
    It's a good answer if he were performing segue properly. But this code won't get called until he does performSegueWithIdentifier:
    – danh
    Dec 13, 2013 at 5:32
  • I actually want him to check out the tutorial for the rest, because that's exactly what he is trying to do.
    – Matt
    Dec 13, 2013 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.