Invoking push segue programmatically and prepareforsegue method

21,181

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
            }
        }
    }
}
Share:
21,181
Jay Mayu
Author by

Jay Mayu

I'm iOS / React Native Developer but I also do some Android, HTML5, Laravel and NodeJS. You can follow me at Twitter @mayooresan or connect with me at LinkedIn If you like my answers and posts then probably you will like my blog Mayu's IT Diary

Updated on July 05, 2022

Comments

  • Jay Mayu
    Jay Mayu almost 2 years

    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?

  • danh
    danh over 10 years
    It's a good answer if he were performing segue properly. But this code won't get called until he does performSegueWithIdentifier:
  • Jay Mayu
    Jay Mayu over 10 years
    Thanks It worked Thos who face similar issue must read this answer too stackoverflow.com/a/17012857/2636704
  • Matt
    Matt over 10 years
    I actually want him to check out the tutorial for the rest, because that's exactly what he is trying to do.