我正在使用Swift即Rock,Paper和Scissors制作iOS应用程序。如何使用Swift 3.0按钮将ViewController的值传递给另一个ViewController
当您按下哪个按钮(带有摇滚图片,图片图片或剪刀图片)时,它会延续到不同的视图控制器,并显示它是平局,胜利还是损失(比较您的数字[即。摇滚按钮是1]由电脑随机数)。但它不会显示结果,因为它需要在按钮中,而不是在按钮和viewDidLoad()之外。
我会给你我的代码片段。我正在使用下一个视图控制器中的函数来返回一个字符串。
所以我需要帮助的是通过将userNum和computerNum传递给按钮中的下一个视图控制器(当按下时)然后调用该函数并返回字符串。
var userNum: Int = 0
var computerNum: Int = 0
@IBAction func rock(_ sender: UIButton) {
userNum = 1
computerNum = (Int)(arc4random_uniform(3) + 1)
} //This next code will be in the other View Controller
func chooseWinner(userNum : Int, computerNum : Int) -> String {
if userNum == computerNum {
return "There is a tie"
}else if userNum == 1 && computerNum == 2{
return "You lost!"
}else if userNum == 1 && computerNum == 3{
return "You won!"
}else if userNum == 2 && computerNum == 1{
return "You won!"
}
else if userNum == 2 && computerNum == 3{
return "You lost!"
}else if userNum == 3 && computerNum == 1{
return "You lost!"
}
else if userNum == 3 && computerNum == 2{
return "You won!"
}else{
return "value"
}
}
尝试使用标识符执行segue ... – Joe
我尝试过,但我不知道如何传递值。 – FusionPointInc