gpt4 book ai didi

ios - 将参数从函数传递给 prepareForSegue

转载 作者:行者123 更新时间:2023-12-01 16:24:26 24 4
gpt4 key购买 nike

我要函数,它们都触发performSegueWithIdentifier以同样的方式。但是根据调用哪个函数,我需要在 prepareForSegue 中传递不同的参数.

就像是

func first() {
// do some stuff
performSegueWithIdentifier("My segue", sender:AnyObject?)
}

func second() {
// do some stuff
performSegueWithIdentifier("My segue", sender:AnyObject?)
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "My segue" {
let destination = segue.destinationViewController as! MyController
if functionFirstWasCalled {
destination.property = value
} else if functionSecondWasCalled {
destination.property = anotherValue
}
}
}

当然,我可以通过从 second() 和 first() 设置 bool 值然后在 prepareForSegue 中检查它们来做到这一点。 - 但也许有一些更优雅的方式来做到这一点?

最佳答案

在 Objective -c 你会做:

     [self performSegueWithIdentifier:@"segueNAme" sender:@"firstMethod"];

您可以在 prepareForSegue 方法中访问此消息
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([sender isEqualToString:@"firstMethod"]) {
//firstMEthod called the segue
}
}

我认为的快速等价物是:
 self.performSegueWithIdentifier("segueNAme", sender: "firstMethod")


func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject) {
if (sender == "firstMethod") {
//firstMEthod called the segue
}
}

我的建议是发送一个字典类型对象,而不是发送一个普通的字符串,该对象包含方法名、类名和其他一些对 future 调试有用的参数。

关于ios - 将参数从函数传递给 prepareForSegue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38443983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com