Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking push segue programmatically and prepareforsegue method

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?

like image 855
Jay Mayu Avatar asked Dec 13 '13 05:12

Jay Mayu


People also ask

How do you do segue programmatically?

Fortunately, it only takes two steps. First, select a segue in your storyboard, then go to the attributes inspector and give it a name such as “showDetail”. Technically the sender parameter is whatever triggered the segue, but you can put whatever you want in there.


1 Answers

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
            }
        }
    }
}
like image 65
Suhit Patil Avatar answered Oct 17 '22 18:10

Suhit Patil