Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.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 如何使用Swift 3.0中的按钮将值从一个ViewController传递到另一个ViewController_Ios_Iphone_Swift_Parameter Passing_Viewcontroller - Fatal编程技术网

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

Ios 如何使用Swift 3.0中的按钮将值从一个ViewController传递到另一个ViewController,ios,iphone,swift,parameter-passing,viewcontroller,Ios,Iphone,Swift,Parameter Passing,Viewcontroller,我正在使用Swift制作iOS应用程序,即石头、布和剪刀 当您按下某个按钮(带有摇滚画、纸片画或剪刀画)时,它将切换到不同的视图控制器,并显示平局、胜利或失败(通过计算机将您的数字[即摇滚按钮为1]与随机数进行比较)。但它不会显示结果,因为它需要在按钮中,而不是在按钮外和viewDidLoad()中 我会给你我的代码片段。我在下一个视图控制器中使用函数返回字符串 所以我所需要的帮助就是将userNum和computerNum传递给按钮中的下一个视图控制器(按下时),然后调用函数并返回字符串 va

我正在使用Swift制作iOS应用程序,即石头、布和剪刀

当您按下某个按钮(带有摇滚画、纸片画或剪刀画)时,它将切换到不同的视图控制器,并显示平局、胜利或失败(通过计算机将您的数字[即摇滚按钮为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"
    }
}

在情节提要编辑器中,按住Ctrl键,同时按住leftclick并将鼠标拖动到要转到的目标视图控制器。释放鼠标后,您可以选择一种序列类型,例如,
show

这将允许您切换到目标视图控制器,而无需像在代码中那样以编程方式创建
iAction

在源代码视图控制器中,添加函数

prepare(对于segue:UIStoryboardSegue,发送方:Any?)

这将允许您检查segue并在segue之前执行任何其他设置

记住在属性输入器中设置序列的标识符

内部源代码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)
        }
    }
}
var userNum: Int!
var computerNum: Int!

override func viewDidLoad() {
    super.viewDidLoad()

    //do wtv you need with your values
    print(chooseWinner(userNum: userNum, computerNum: computerNum))
}
内部目标视图控制器

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)
        }
    }
}
var userNum: Int!
var computerNum: Int!

override func viewDidLoad() {
    super.viewDidLoad()

    //do wtv you need with your values
    print(chooseWinner(userNum: userNum, computerNum: computerNum))
}

在情节提要编辑器中,按住Ctrl键,同时按住leftclick并将鼠标拖动到要转到的目标视图控制器。释放鼠标后,您可以选择一种序列类型,例如,
show

这将允许您切换到目标视图控制器,而无需像在代码中那样以编程方式创建
iAction

在源代码视图控制器中,添加函数

prepare(对于segue:UIStoryboardSegue,发送方:Any?)

这将允许您检查segue并在segue之前执行任何其他设置

记住在属性输入器中设置序列的标识符

内部源代码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)
        }
    }
}
var userNum: Int!
var computerNum: Int!

override func viewDidLoad() {
    super.viewDidLoad()

    //do wtv you need with your values
    print(chooseWinner(userNum: userNum, computerNum: computerNum))
}
内部目标视图控制器

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)
        }
    }
}
var userNum: Int!
var computerNum: Int!

override func viewDidLoad() {
    super.viewDidLoad()

    //do wtv you need with your values
    print(chooseWinner(userNum: userNum, computerNum: computerNum))
}

尝试使用标识符执行segue…我尝试过,但不知道如何传递值。尝试使用标识符执行segue…我尝试过,但不知道如何传递值。有吗?而不是任何对象?可以它不允许我在不删除覆盖的情况下执行任何对象。观察得好!我没意识到他们从AnyObject变为Any。id是否作为Any导入?而不是Swift 3中的任何对象。有吗?而不是任何对象?可以它不允许我在不删除覆盖的情况下执行任何对象。观察得好!我没意识到他们从AnyObject变为Any。id是否作为Any导入?而不是Swift 3中的任何对象。