私は、Swiftというロック、ペーパー、はさみを使用してiOSアプリケーションを作成しています。Swift 3.0でボタンを使用して、ViewControllerから別のViewControllerに値を渡す方法
ロックピクチャ、ペーパーピクチャ、またははさみピクチャのいずれかのボタンを押すと、別のView Controllerに切り替わり、同調、勝ち、または喪失と表示されます(つまり、ロックボタンは1]をコンピュータの乱数に変換する)。しかし、結果がボタンに表示され、ボタンやviewDidLoad()には表示されないため、結果は表示されません。
私のコードのスニペットを教えてあげます。私は文字列を返すために次のView Controllerで関数を使用しています。
私は、userNumとcomputerNumをボタンの次のView Controller(押されたとき)に渡して、関数を呼び出して文字列を返すだけでいいです。ストーリーボードエディタで
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"
}
}
識別子でセグを実行してみてください... – Joe
私はそれを試みましたが、私は値を通す方法を知らない。 – FusionPointInc