以编程方式调用push segue和prepareforsegue方法

我正在以编程方式调用push segue ,如下所示

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

但我的问题是我想将这个segue的一些信息发送到目标viewcontroller。 但是方法- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender根本没有被解雇。

这就是准备方法的方法

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

我在这里想念的是什么?

didSelectRowAtIndexPath方法调用performSegueWithIdentifier :.

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

在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 } } } 

斯威夫特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 } } } } 

这是您要查找的信息:

  (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将与[self perform segue]一起调用,这是你应该仔细研究的教程 。