gpt4 book ai didi

ios - Segue 没有被转入(swift 3)

转载 作者:行者123 更新时间:2023-11-30 12:46:52 24 4
gpt4 key购买 nike

我正在制作一个游戏,当按下按钮两次时,它将转移到另一个 View Controller 。现在用户的 react 时间是在 View Controller a 中计时的。我希望将时间转移到 View Controller b 中,并且在 View Controller b 中时将时间减去 1。现在时间没有被转移。当按钮被按下两次时, View Controller 会发生变化,但文本不会被传输。

import UIKit

class ViewController: UIViewController {

@IBOutlet var labelx: UILabel!
@IBOutlet var startx: UIButton!
@IBOutlet var pressSoccerBall: UIButton!
var level = 0
var timer: Timer?
var isRunning: Bool {
get {
return timer != nil
}
}
var counter = 0.0

override func viewDidLoad() {
super.viewDidLoad()
labelx.text = String(format: "%.1f", counter)
startx.isEnabled = true
}

@IBAction func startTimer(_ sender: Any) {
if isRunning {
return
}
refreshTimer()
}

@IBAction func PressSoccerBall(_ sender: Any) {
level += 1
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let DestViewController : winViewController = segue.destination as! winViewController
DestViewController.LebelText = labelx.text!
}

func refreshTimer() {

if let timer: Timer = timer {
timer.invalidate()
}
timer = Timer.scheduledTimer(timeInterval: 0.1,target: self,selector: #selector(updateTimer),userInfo: nil, repeats: true)
startx.isEnabled = false
}

func updateTimer() {
counter += 0.1
labelx.text = String(format: "%.1f", counter)
if counter < 9.9 && level == 2 {
let nextc = self.storyboard?.instantiateViewController(withIdentifier: "winViewController") as? winViewController
self.present(nextc!, animated: true, completion: nil)
}
}
}

pic of vc a

查看 Controller B

import UIKit

class winViewController: UIViewController {

@IBOutlet var winningLabel: UILabel!
public var LebelText: String?

override func viewDidLoad() {
super.viewDidLoad()

steve()
}

func steve(){

guard let unwrapedText = self.LebelText else {
return
}
if let myInt = Double(unwrapedText){
let myInt = myInt - 1
self.winningLabel.text = String(myInt)

} else {
return
}
}

}

PIC OF VIEW CONTROLLER B

最佳答案

你没有使用segues。您正在创建 winViewController ,然后调用 Present 来显示它。因此,prepareForSegue 永远不会被调用。

在呈现之前,只需在 nextc 上设置 LebelText 属性即可:

func updateTimer() {
counter += 0.1
labelx.text = String(format: "%.1f", counter)
if counter < 9.9 && level == 2 {
if let nextc = self.storyboard?.instantiateViewController(withIdentifier: "winViewController") as? winViewController {
nextc.LebelText = labelx.text
self.present(nextc, animated: true, completion: nil)
}
}
}

关于ios - Segue 没有被转入(swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41528296/

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