ios 以编程方式执行 Segue 并将参数传递给目标视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9248798/
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
Perform Segue programmatically and pass parameters to the destination view
提问by yassassin
in my app I've a button that performs a segue programmatically:
在我的应用程序中,我有一个以编程方式执行 segue 的按钮:
- (void)myButtonMethod
{
//execute segue programmatically
[self performSegueWithIdentifier: @"MySegue" sender: self];
}
I would like to know if there is a way to reference the destination view and to pass it some parameters.
我想知道是否有办法引用目标视图并向其传递一些参数。
I know that in prepareForSegue
method, I can refer to it with:myDestinationViewController *vc = [segue destinationViewController];
, but I don't know how to this executing the segue programmatically.
我知道在prepareForSegue
方法中,我可以用: 来引用它myDestinationViewController *vc = [segue destinationViewController];
,但我不知道如何以编程方式执行 segue。
Do you have any ideas?
你有什么想法?
Thanks, yassa
谢谢,亚萨
UPDATE:
更新:
I'm sorry for this question!!! I simply discovered that, even if the segue is invoked programmatically, the prepareForSegue
method is called anyway and so it is possible to pass parameters in the same usual way.
我很抱歉这个问题!!!我只是发现,即使以编程prepareForSegue
方式调用 segue,无论如何都会调用该方法,因此可以以相同的通常方式传递参数。
回答by trapper
The answer is simply that it makes no difference how the segue is triggered.
答案很简单,segue 的触发方式没有任何区别。
The prepareForSegue:sender:
method is called in any case and this is where you pass your parameters across.
prepareForSegue:sender:
在任何情况下都会调用该方法,这是您传递参数的地方。
回答by user1723341
Old question but here's the code on how to do what you are asking. In this case I am passing data from a selected cell in a table view to another view controller.
老问题,但这是关于如何做你所问的代码。在这种情况下,我将数据从表视图中选定的单元格传递到另一个视图控制器。
in the .h file of the trget view:
在 trget 视图的 .h 文件中:
@property(weak, nonatomic) NSObject* dataModel;
in the .m file:
在 .m 文件中:
@synthesize dataModel;
dataModel
can be string
, int
, or like in this case it's a model that contains many items
dataModel
可以是string
, int
, 或者像在这种情况下它是一个包含许多项目的模型
- (void)someMethod {
[self performSegueWithIdentifier:@"loginMainSegue" sender:self];
}
OR...
或者...
- (void)someMethod {
UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeController"];
[self.navigationController pushViewController: myController animated:YES];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"storyDetailsSegway"]) {
UITableViewCell *cell = (UITableViewCell *) sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSDictionary *storiesDict =[topStories objectAtIndex:[indexPath row]];
StoryModel *storyModel = [[StoryModel alloc] init];
storyModel = storiesDict;
StoryDetails *controller = (StoryDetails *)segue.destinationViewController;
controller.dataModel= storyModel;
}
}
回答by Jonas Deichelmann
Swift 4:
斯威夫特 4:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ExampleSegueIdentifier" {
if let destinationVC = segue.destination as? ExampleSegueVC {
destinationVC.exampleString = "Example"
}
}
}
Swift 3:
斯威夫特 3:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ExampleSegueIdentifier" {
if let destinationVC = segue.destinationViewController as? ExampleSegueVC {
destinationVC.exampleString = "Example"
}
}
}
回答by karthik1729
I understand the problem of performing the segue at one place and maintaining the state to send parameters in prepare for segue.
我了解在一个地方执行转场并保持状态以发送参数以准备转场的问题。
I figured out a way to do this. I've added a property called userInfoDict to ViewControllers using a category. and I've override perform segue with identifier too, in such a way that If the sender is self(means the controller itself). It will pass this userInfoDict to the next ViewController.
我想出了一种方法来做到这一点。我使用类别向 ViewControllers 添加了一个名为 userInfoDict 的属性。并且我也覆盖了使用标识符执行 segue 的方式,如果发件人是自己(意味着控制器本身)。它会将这个 userInfoDict 传递给下一个 ViewController。
Here instead of passing the whole UserInfoDict you can also pass the specific params, as sender and override accordingly.
在这里,除了传递整个 UserInfoDict,您还可以传递特定的参数,作为发送者并相应地覆盖。
1 thing you need to keep in mind. don't forget to call super method in ur performSegue method.
您需要牢记的 1 件事。不要忘记在你的 performSegue 方法中调用 super 方法。
回答by Pokotuz
In case if you use new swift version.
如果您使用新的 swift 版本。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ChannelMoreSegue" {
}
}