[英]How to pass values from a ViewController to another ViewController using a button with Swift 3.0
I am making an iOS application using Swift that is Rock, Paper, and Scissors. 我正在使用Swift开发的iOS应用程序,即Rock,Paper和Scissors。
When you press which a button (with Rock picture, Paper picture, or Scissors picture), it will segue to a different View Controller and display it is a tie, a win, or a loss (compares your number [ie. Rock button is 1] to a random number by the computer). 当您按下哪个按钮(带有Rock图片,Paper图片或剪刀图片)时,它将选择另一个View Controller,并显示是平局,获胜还是失败(比较您的数字[即Rock button是1]到计算机随机数)。 But it won't display the result since it needs to be in the button, not out of the button and in the viewDidLoad().
但是它不会显示结果,因为它需要在按钮中,而不是按钮之外和viewDidLoad()中。
I'll give you snippets of my code. 我将向您提供我的代码段。 I am using functions in the next View Controller to return a string.
我在下一个View Controller中使用函数来返回字符串。
So all I need help is with passing userNum and computerNum to the next View Controller in the button (when pressed) to then call the function and return the String. 因此,我需要帮助的是将userNum和computerNum传递给按钮中的下一个View Controller(按下时),然后调用该函数并返回String。
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"
}
}
In the Storyboard Editor, hold Ctrl
while holding down leftclick and drag your mouse to the destination view controller you want to segue to. 在情节提要编辑器中,按住
Ctrl
同时按住leftclick并将鼠标拖到要选择的目标视图控制器。 You can choose a segue type once you release your mouse, eg show
. 释放鼠标后,您可以选择segue类型,例如
show
。
This will allow you to segue to your destination view controller without having to create an IBAction
programatically, like in your code. 这将使您可以选择到目标视图控制器,而不必像在代码中那样以编程方式创建
IBAction
。
In the source view controller, add the function 在源视图控制器中,添加功能
prepare(for segue: UIStoryboardSegue, sender: Any?)
which will allow you to check for a segue and do any additional setup before segueing. 这将使您可以在进行序列检查之前检查序列并进行任何其他设置。
Remember to set the identifier for your segue in the Attribute Inpector 请记住在属性检查器中为您的序列设置标识符
Inside source viewcontroller 内部源ViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "destination" {
//OtherViewController is a placeholder for the viewcontroller class of your destination viewcontroller
if let destinationVC = segue.destinationViewController as? OtherViewController {
destinationVC.userNum = 1
destinationVC.computerNum = (Int)(arc4random_uniform(3) + 1)
}
}
}
Inside destination view controller 内部目标视图控制器
var userNum: Int!
var computerNum: Int!
override func viewDidLoad() {
super.viewDidLoad()
//do wtv you need with your values
print(chooseWinner(userNum: userNum, computerNum: computerNum))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.