gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:39:13 24 4
gpt4 key购买 nike

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

我尝试过使用prepare 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 和输入数字之间的随机值,以便我可以继续工作并向我的应用程序添加更多功能(例如打印这些随机值等)

最佳答案

View Controller

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

关于swift - 如何在另一个 View Controller 中使用主视图 Controller 的函数中定义的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57228817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com