【发布时间】:2020-03-30 13:22:44
【问题描述】:
我有一个名为“ViewController.swift”的主 VC,它与 Main.storyboard 中的 ViewController 场景绑定。 然后我想要另一个屏幕,它显示我的 ViewController.swift 中的条件何时满足。 因此,我得到了一个 Gameover.storyboard,它有一个带有 Storyboard ID“overID”的 VC 场景以及一个用于处理 GameOver 场景的“GameoverViewController.swift”。
这是我在 ViewController.swift 中的代码,当满足以下条件时,它应该会打开 Gameover.storyboards VC 场景:
import UIKit
class ViewController: UIViewController {
var wantedLiter = 10.00
var timer : Timer?
var actualLiter = 0.00
var timerReset = true
var preisProLiter = 1.26
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
let storyBoard: UIStoryboard = UIStoryboard(name: "Gameover", bundle: nil)
let gameoverViewController = storyBoard.instantiateViewController(withIdentifier: "overID") as! GameoverViewController
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@IBOutlet weak var preisliterLabel: UILabel!
@IBOutlet weak var euroLabel: UILabel!
@IBOutlet weak var numberLabel: UILabel!
@IBOutlet weak var numberButton: UIButton!
@IBOutlet weak var confirmButton: UIButton!
@IBAction func confirm(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
numberLabel.text = String(round(1000*actualLiter)/1000) //initial value
preisliterLabel.text = String(preisProLiter)
// Do any additional setup after loading the view.
}
@IBAction func holdingTheButton(_ sender: Any) {
print("I am holding")
timerReset = false // reset to false since you are holding the button
guard timer == nil else { return }
timer = Timer.scheduledTimer(timeInterval: 0.005, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}
@IBAction func buttonReleased(_ sender: Any) {
print("button released")
if (wantedLiter == actualLiter){
print("WIN!")
actualLiter = 0;
}
//startTime = 0.00
timer?.invalidate()
timer = nil
// timerReset = true // reset to true since you released.
}
@objc func updateTime(){
//update label every second
print("updating label ")
if (wantedLiter <= actualLiter){
print("Ooops")
actualLiter = 0;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
self.present(gameoverViewController, animated: true, completion: nil)
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
actualLiter += 0.01
numberLabel.text = String(round(1000*actualLiter)/1000)
euroLabel.text = String((round(100*actualLiter*preisProLiter)/100))
}
}
现在为您提供了一些屏幕截图,这可能会有所帮助。
如果您需要更多屏幕,请询问。
现在,我收到以下错误:
1) ViewController.swift 上部标有“!!!...”
不能在属性初始化器中使用实例成员“storyBoard”;属性初始化器在“self”可用之前运行
2) ViewController.swift
无法将“gameoverViewController.Type”类型的值转换为预期的参数类型“UIViewController”
3) ViewController.swift 上半部分:
使用未声明的类型“GameoverViewController”
这就是我更改代码的方式:
import UIKit
class ViewController: UIViewController {
var wantedLiter = 10.00
var timer : Timer?
var actualLiter = 0.00
var timerReset = true
var preisProLiter = 1.26
let storyBoard: UIStoryboard = UIStoryboard(name: "Gameover", bundle: nil)
var gameoverViewController: GameoverViewController!
@IBOutlet weak var preisliterLabel: UILabel!
@IBOutlet weak var euroLabel: UILabel!
@IBOutlet weak var numberLabel: UILabel!
@IBOutlet weak var numberButton: UIButton!
@IBOutlet weak var confirmButton: UIButton!
@IBAction func confirm(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
gameoverViewController = storyBoard.instantiateViewController(withIdentifier: "overID") as? GameoverViewController
numberLabel.text = String(round(1000*actualLiter)/1000) //initial value
preisliterLabel.text = String(preisProLiter)
// Do any additional setup after loading the view.
}
@IBAction func holdingTheButton(_ sender: Any) {
print("I am holding")
timerReset = false // reset to false since you are holding the button
guard timer == nil else { return }
timer = Timer.scheduledTimer(timeInterval: 0.005, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}
@IBAction func buttonReleased(_ sender: Any) {
print("button released")
if (wantedLiter == actualLiter){
print("WIN!")
actualLiter = 0;
}
//startTime = 0.00
timer?.invalidate()
timer = nil
// timerReset = true // reset to true since you released.
}
@objc func updateTime(){
//update label every second
print("updating label ")
if (wantedLiter <= actualLiter){
print("Ooops")
actualLiter = 0;
self.present(gameoverViewController as! UIViewController, animated: true, completion: nil)
}
actualLiter += 0.01
numberLabel.text = String(round(1000*actualLiter)/1000)
euroLabel.text = String((round(100*actualLiter*preisProLiter)/100))
}
}
(不工作)
我将 GameoverViewController 类型更改为 UIViewController。
但是我的错字在哪里? (G 而不是 g)?
【问题讨论】:
标签: swift viewcontroller