简体   繁体   English

如何在Swift 3.0中使用按钮将值从ViewController传递到另一个ViewController

[英]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.

相关问题 如何使用Swift 3将数据从ViewController A传递到另一个ViewController B - How to pass data from viewcontroller A to another viewcontroller B using swift 3 如何在不使用按钮的情况下快速将数据传递到另一个ViewController - How to pass data to another ViewController without using a button in swift 如何快速将图像从一个视图控制器传递到另一个视图控制器? - How to pass an image from one viewcontroller to another viewcontroller in swift? 如何从ViewController访问Swift 3中的另一个viewController - How to access from viewController another viewController in Swift 3 如何将 tableViewCell 中的 Button 文本打印/传递到 Swift 中的另一个 ViewController? - How to print/pass the text of a Button in a tableViewCell to another ViewController in Swift? Swift 3将数据从一个ViewController传递到另一个ViewController - Swift 3 pass data from one ViewController to another ViewController 如何使用Swift 4.2设置标志并将某些值从Popup当前模型viewcontroller传递到Tabbar主Viewcontroller? - How to set flag and pass some values from Popup present model viewcontroller to Tabbar Main Viewcontroller Using Swift 4.2? 如何在Swift上从MapView传递解析的PFUser到另一个ViewController? - How to pass (grab) a Parse `PFUser` from `MapView` to another `ViewController` on Swift? 使用通知将NSString值从一个ViewController传递到另一个ViewController? - Pass NSString values from one ViewController to Another using Notifications? 如何将数据从模态呈现的 ViewController 传递到 swift 5 中的父 ViewController? - How to pass the data from modally presented ViewController to parent ViewController in swift 5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM