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

时间:2013-12-13 05:16:49

标签: ios iphone objective-c uistoryboard segue

我正在以编程方式调用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];
    }
}

我在这里缺少什么?

2 个答案:

答案 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