ios 以编程方式调用 push segue 和 prepareforsegue 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20559581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invoking push segue programmatically and prepareforsegue method
提问by Jay Mayu
I'm invoking push segue
programmatically as below
我以push segue
编程方式调用如下
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.
但我的问题是我想用这个 segue 将一些信息发送到目标视图控制器。但是该方法- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
根本没有被解雇。
this is how the prepareforsegue method
这就是 prepareforsegue 方法的方式
- (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?
我在这里缺少什么?
回答by Suhit Patil
Call performSegueWithIdentifier
: from your didSelectRowAtIndexPath
method
调用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];
}
}
in Swift 2.3
在 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
斯威夫特 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
}
}
}
}
回答by Matt
This is the information that you are looking for :
这是您正在寻找的信息:
(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 will get called along with [self perform segue]
and this is the tutorialyou should definitely look into.
prepareForSegue 将被调用[self perform segue]
,这是您绝对应该研究的教程。