以编程方式调用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]
一起调用,这是你应该仔细研究的教程 。
- 如何从NSURLSessionTask禁用caching
- 错误:目标无法运行:debuggingcom.apple.Preferences的权限被拒绝
- 领域通知令牌在后台线程
- 在tableView中select多个单元格,并将选中的单元格显示在另一个tableView单元格中作为标签?
- Autolayout混淆:与WebView和页眉/页脚视图的ScrollView的约束
- 实验:iOS网络缓存
- iOS CoreLocation:有可能知道位置数据是来自GPS还是来自Wifi?
- UITableViewCell + Autolayout + RTL
- iDevice for Dummies:设备是否可以分配多个设置(个人/企业)?