我正在以编程方式调用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];
}
}
我在这里缺少什么?
答案 0 :(得分:21)
通过performSegueWithIdentifier
方法
didSelectRowAtIndexPath
:.
- (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
}
}
}
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)
这是您要查找的信息:
(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]
一起被调用
这是你应该留意的tutorial。