gpt4 book ai didi

swift - 实例化 View Controller 时出错

转载 作者:行者123 更新时间:2023-11-30 11:19:04 25 4
gpt4 key购买 nike

当我尝试使用下面的代码(在 viewDidLoad 部分)实例化我的 ViewController 时,在 self.present 行上运行应用程序时,我得到一个 Thread 1: EXC_BAD_ACCESS 。 (我是编码初学者)

编辑:所以我在这里提供了完整的代码,看看哪里出了问题。我现在将代码转移到第二个 View Controller 的 viewDidLoad 中。现在应用程序启动了,但是当我按下“enter”按钮时,我收到“Thread 1: Fatal error: Unexpectedlyfound nil while unwrapping anOptional value”错误..

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var progressBarView: ProgressBarView!

@IBOutlet weak var progressBarView1: ProgressBarView1!


override func viewDidLoad() {
super.viewDidLoad()
// updaten van de progressbar
progressBarView.progress = CGFloat(percProgress())
progressBarView1.progress = CGFloat(0.5)

// een constante maken die de daystogocalc Int van de berekening verderop in een String cast
let daysToGoCalcString = String(daysToGoCalc())

// updaten van het label daysToGo
daysToGo.text = "Still \(daysToGoCalcString) days to go"

// updaten van het label textPercentage
let textPercentage = NSString(format: "%.0f", (percProgress() * 100))
percentageDays.text = String(textPercentage) + "%"

}






@IBOutlet weak var daysToGo: UILabel!

@IBOutlet weak var percentageDays: UILabel!

@IBOutlet weak var firstGoalLabel: UILabel!


@IBAction func unwindToGoals(unwindSegue: UIStoryboardSegue) {

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// Automatische datumberekening
let today = Date()
let calendar = Calendar.current
// Dagnummer van het jaar
var todayNumber: Int {
get {
return calendar.ordinality(of: .day, in: .year, for: today)!
}
}
// FLOAT MAKER: onderstaande maakt een Float van het dagnummer (nodig voor de Progress Bar uiteindelijk)
var todayFloat: Float {
get {
return Float(todayNumber)
}
}
// KWARTAAL PROGRESSIE: deze functie checkt eerst in welk kwartaal je zit, en vervolgens de voortgang in dat kwartaal in een float
func percProgress() -> Float {

if todayNumber < 91 {
return todayFloat / 90
}
else if todayNumber < 182 {
return (todayFloat - 90) / 91
}
else if todayNumber < 274 {
return (todayFloat - 181) / 92
}
else {
return (todayFloat - 273) / 92
}
}

// Bereken aantal dagen te gaan in kwartaal
func daysToGoCalc() -> Int {

if todayNumber < 91 {
return 90 - todayNumber
}
else if todayNumber < 182 {
return 181 - todayNumber
}
else if todayNumber < 274 {
return 273 - todayNumber
}
else {
return 365 - todayNumber
}
}


}




// SECOND VIEW CONTROLLER

class SecondViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

firstGoaltext.delegate = self
firstGoalStart.delegate = self
firstGoalEnd.delegate = self

secondGoaltext.delegate = self
secondGoalStart.delegate = self
secondGoalEnd.delegate = self

thirdGoaltext.delegate = self
thirdGoalStart.delegate = self
thirdGoalEnd.delegate = self

fourthGoaltext.delegate = self
fourthGoalStart.delegate = self
fourthGoalEnd.delegate = self

fifthGoaltext.delegate = self
fifthGoalStart.delegate = self
fifthGoalEnd.delegate = self

// INSTANTIATE VIEWCONTROLLER
let storyboard1 = UIStoryboard(name: "Main", bundle: nil)
var firstController: UIViewController {
get {
return storyboard1.instantiateViewController(withIdentifier: "myViewControllerIdentifier")
}
}

self.present(firstController, animated: true, completion: nil)

}



@IBOutlet weak var firstGoaltext: UITextField!
@IBOutlet weak var firstGoalStart: UITextField!
@IBOutlet weak var firstGoalEnd: UITextField!

@IBOutlet weak var secondGoaltext: UITextField!
@IBOutlet weak var secondGoalStart: UITextField!
@IBOutlet weak var secondGoalEnd: UITextField!

@IBOutlet weak var thirdGoaltext: UITextField!
@IBOutlet weak var thirdGoalStart: UITextField!
@IBOutlet weak var thirdGoalEnd: UITextField!

@IBOutlet weak var fourthGoaltext: UITextField!
@IBOutlet weak var fourthGoalStart: UITextField!
@IBOutlet weak var fourthGoalEnd: UITextField!

@IBOutlet weak var fifthGoaltext: UITextField!
@IBOutlet weak var fifthGoalStart: UITextField!
@IBOutlet weak var fifthGoalEnd: UITextField!

@IBAction func enter(_ sender: Any!) {

let vc1 = ViewController()
vc1.firstGoalLabel.text = firstGoaltext.text
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
firstGoalStart.resignFirstResponder()
}

}



extension SecondViewController : UITextFieldDelegate {

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}

}

我在这里做错了什么?

最佳答案

尝试在“viewDidAppear”而不是“viewDidLoad”中运行代码片段

关于swift - 实例化 View Controller 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51485134/

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