如何在另一个View Controller的主View Controller中的函数中使用定义的变量?

时间:2019-07-27 03:03:49

标签: swift uiviewcontroller

我正在开发一个简单的游戏(我的第一个iOS应用程序!),并试图在两个View Controller之间链接一个变量。在第一个视图控制器中,我有一个文本字段,用户可以在其中输入他们选择的任何数字。在第二个View Controller中,我希望用户能够生成1到他们通过按按钮输入的数字之间的任何数字,并且能够继续这样做。但是,我无法使用“ upperBound”变量来保存ViewController2中用户输入的值。

我已经尝试过使用prese for segue进行操作,但是它无法正常工作,而且我一直在探究stackoverflow并尝试了几种方法,但并不完全知道自己在做什么。

(已更新)ViewController:

class ViewController: UIViewController, UITextFieldDelegate {

//MARK: Properties

@IBOutlet weak var numberOfPages: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    // Handle the text field’s user input through delegate callbacks.
    numberOfPages.delegate = self

}
//MARK: UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Hide the keyboard.
textField.resignFirstResponder()
return true
}

func textFieldDidEndEditing(_ textField: UITextField) {

//Save number entered and then randomly select a number within bounds
}


//MARK: Actions

var upperBound: Int?
@IBAction func setUpperBound(_ sender: UIButton) {
upperBound = Int(numberOfPages.text!)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    // Create a variable that you want to send
    var newUpperBound = Int(upperBound!)

    // Create a new variable to store the instance of ViewController2
    let destinationVC = segue.destination as! ViewController2
    destinationVC.upperBound = newUpperBound
}

}

(已更新)ViewController2:

class ViewController2: UIViewController, UITextFieldDelegate {


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/

//Mark: Actions
@IBAction func roller(_ sender: UIButton) {
    //Generate random number
    let randomNumber = Int.random(in: 0 ..< upperBound)
}
var upperBound: Int?
}

使用此代码,我在ViewController2的第34行上看到一条错误,显示为“使用未解决的标识符upperBound”。此外,ViewController的第40行上出现一个问题,内容为“从未使用过不可变值upperBound”。我希望能够在1和输入的数字之间生成一个随机值,以便我可以继续工作并向我的应用添加更多功能(例如打印这些随机值等)

1 个答案:

答案 0 :(得分:0)

ViewController

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var numberOfPages: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    numberOfPages.delegate = self
}

//MARK: UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    // Hide the keyboard.
    textField.resignFirstResponder()
    return true
}

func textFieldDidEndEditing(_ textField: UITextField) {
    //Save number entered and then randomly select a number within bounds
}

//MARK: Actions
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if numberOfPages.text == ""{
        print("Please enter number")
        return
    }
    let upperBound: Int? = Int(numberOfPages.text ?? "0")

    if upperBound != 0{
        if segue.identifier == "mySegue"{
            let vc = segue.destination as! ViewController2
            vc.upperBound = upperBound
        }
    }
}
}

ViewController2

import UIKit

class ViewController2: UIViewController {

@IBOutlet weak var lbl_UpperBound: UILabel!
@IBOutlet weak var btn_Generate: UIButton!
@IBOutlet weak var lbl_Random: UILabel!

var upperBound: Int?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    lbl_UpperBound.text = "Upper Bound - \(upperBound ?? 0)"
    btn_Generate.addTarget(self, action: #selector(roller), for: .touchUpInside)
    lbl_Random.text = ""
}


@objc func roller(_ sender: UIButton) {
    //Generate random number
    let randomNumber = Int.random(in: 0 ..< (upperBound ?? 1))
    lbl_Random.text = "\(randomNumber)"
}

}

别忘了给Segue命名

enter image description here