当我尝试使用下面的代码(在viewDidLoad部分中)实例化ViewController时,在self.present行上运行应用程序时得到线程1:EXC_BAD_ACCESS。 (我是编码的初学者)
编辑:因此,我在这里提供我的整个代码,以查看出现问题的地方。我现在将代码移到了第二个View Controller的viewDidLoad上。现在,应用程序启动了,但是当我按下“输入”按钮时,出现“线程1:致命错误:在展开可选值时意外发现nil”错误。
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
}
}
我在这里做错了什么?
答案 0 :(得分:0)
尝试在“ viewDidAppear”而不是“ viewDidLoad”中运行代码片段