Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何制作两个segue函数?_Ios_Swift_Segue - Fatal编程技术网

Ios 如何制作两个segue函数?

Ios 如何制作两个segue函数?,ios,swift,segue,Ios,Swift,Segue,如何制作两个segue覆盖函数 @IBAction func doneBut(_ sender: UIButton) { print("Done") if pointInput.text!.characters.count < 500 { self.performSegue(withIdentifier: "toResult", sender: self) } else { self.performSegue(withIden

如何制作两个segue覆盖函数

@IBAction func doneBut(_ sender: UIButton) {
    print("Done")
    if pointInput.text!.characters.count < 500 {
        self.performSegue(withIdentifier: "toResult", sender: self)
    }
    else {
        self.performSegue(withIdentifier: "toWinner", sender: self)
    }

}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toResult" {
        if segue.destination is ResultViewController {

        }
    }
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toWinner" {
        if segue.destination is ResultViewController {

        }
    }
}
@IBAction func doneBut(uu发送方:ui按钮){
打印(“完成”)
如果pointInput.text!.characters.count<500{
self.performsgue(标识符为“toResult”,发送方为self)
}
否则{
self.performsgue(标识符为“towniner”,发送方为self)
}
}
覆盖功能准备(对于segue:UIStoryboardSegue,发送方:有吗?){
如果segue.identifier==“toResult”{
如果segue.destination是ResultViewController{
}
}
}
覆盖功能准备(对于segue:UIStoryboardSegue,发送方:有吗?){
如果segue.identifier==“toWinner”{
如果segue.destination是ResultViewController{
}
}
}
当我执行另一个覆盖函数时,出现了一个错误

“准备(发件人:)”已被重写


您只能覆盖它一次。使用
if
语句匹配不同的标识符。试试这个

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toResult" {
        if segue.destination is ResultViewController {

        }
    } else if segue.identifier == "toWinner" {
        if segue.destination is ResultViewController {

        }
    }
}

不,您不应该两次编写同一个函数。这正是这个错误所说的

“准备(发件人:)”已被重写

该函数已被重写

试着这样写:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toResult" {
        if segue.destination is ResultViewController {

        }
    else  if segue.identifier == "toWinner" {
        if segue.destination is ResultViewController {

        }
      }
 }

您必须仅覆盖一次
prepare(for
),并通过
标识符上的
开关
来区分分段:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch segue.identifier {
      case "toResult":
        let destination = segue.destination as! ResultViewController 
        // prepare something

      case "toWinner":
        let destination = segue.destination as! ResultViewController 
        // prepare something

      default: break
    }
}
要在
Int
值上切换输入,请使用此版本并进行类型检查:

@IBAction func doneBut(_ sender: UIButton) {
    print("Done")
    guard let inputText = pointInput.text, let inputInt = Int(inputText) else { 
          // inform the user about the error if needed
          return 
    }
    if inputInt < 500 {
        self.performSegue(withIdentifier: "toResult", sender: self)
    }  else {
        self.performSegue(withIdentifier: "toWinner", sender: self)
    }
}
@IBAction func doneBut(uu发送方:ui按钮){
打印(“完成”)
保护let inputInt=pointInput.text,let inputInt=Int(inputText)else{
//如果需要,将错误告知用户
返回
}
如果输入<500{
self.performsgue(标识符为“toResult”,发送方为self)
}否则{
self.performsgue(标识符为“towniner”,发送方为self)
}
}

让我们试着从错误消息中了解您的问题。

“准备(发件人:)”已被重写

在同一个类中,同一个方法(定义相同)不能重复一次,因为它不利于OOP。


假设早上你父亲说“早上8点你无论如何都得去上学”
过了几次,你母亲来告诉你,“早上8点你必须去你姑妈家帮她。”

现在你也变得困惑和有点愤怒,因为你不知道早上8点要做什么?
您的编译器也会发生这种情况,并显示错误,请告诉我在调用此函数“prepare(for:sender:)”

您已经知道问题的解决方案,因此不在此处发布。
在这里,我只想让你知道,如果我们理解得很好,解决我们的问题是很容易的。

如我的观点对你有很好的解决方案,

如果用户在输入500并且更多的分割到VC A,否则如果输入小于500 - SeGuE到VC B(SiguE为一个按钮)!我怎么办呢?请用你的<代码> iBase<代码>在问题和<代码>准备好。(对于segue
,在我的回答中。如果您不需要将任何参数传递给目标视图控制器,您可以完全省略
prepare(对于segue
)。是否正确>我理解并为回答添加了更可靠的解决方案。