我制作了我的第一个 iOS 应用程序。但是有两个烦人的错误我无法摆脱。我希望有人能帮助我!
该应用程序应该训练阅读乐谱。用户指定他的乐器和级别(在之前的 View Controller 上),并基于此,它在屏幕上以乐谱形式放置随机音符。用户应匹配文本字段中的这些注释,应用程序会跟踪分数并在十个正确答案后提升一个级别。
但是,不知何故,我在生成随机音符的函数上遇到了问题。由于某种原因,该函数被调用两次,第一次是它生成注释,将它们保存在一个全局变量中,并用注释创建标签。第二次,它改变了全局变量而不是标签。这次它返回以下错误消息:2018-09-29 23:08:37.279170+0200 MyProject[57733:4748212] Warning: Attempt to present <MyProject.ThirdViewController: 0x7fc709125890> on <MyProject.SecondViewController: 0x7fc70900fcd0> whose view is not in the window hierarchy!
因此,用户在屏幕上回答了问题,但应用认为这是错误的答案,因为它存储了第二个答案。
用户第二次回答问题时,该函数只被调用一次,但文本字段中的读数不会更新为新值,而是与第一个问题保持一致。
这是给出问题的代码:
import UIKit
class ThirdViewController: UIViewController
{
// snip
func setupLabels() {
// snip
// here the random notes are created, this is function is called multiple times for some reason
let antwoord = Noten()
let antwoordReturn = antwoord.generateNoten(instrument: instrument, ijkpunt: ijkpunt, aantalNoten: aantalNoten-1)
let sleutel = antwoordReturn.0
let heleOpgave = antwoordReturn.1
print(heleOpgave)
print(PassOpgave.shared.category)
let heleOpgaveNummers = antwoordReturn.2
// snip
var a = 0
while a < aantalNoten {
// the labels are created, no problems there
let myTekstveld = UITextField(frame: CGRect(x: labelX, y: labelY + 150, width: labelWidth, height: labelHeight / 2))
myTekstveld.backgroundColor = UIColor.white
myTekstveld.textAlignment = .center
myTekstveld.placeholder = "?"
myTekstveld.keyboardType = UIKeyboardType.default
myTekstveld.borderStyle = UITextField.BorderStyle.line
myTekstveld.autocorrectionType = .no
myTekstveld.returnKeyType = UIReturnKeyType.done
myTekstveld.textColor = UIColor.init(displayP3Red: CGFloat(96.0/255.0), green: CGFloat(35.0/255.0), blue: CGFloat(123.0/255.0), alpha: 1)
myTekstveld.delegate = self as? UITextFieldDelegate
myTekstveld.tag = a + 1
view.addSubview(myTekstveld)
a += 1
labelX += labelWidth
}
// the button is created
}
override func viewDidLoad()
{
super.viewDidLoad()
// snip
setupLabels()
}
@objc func buttonAction(sender: UIButton!) {
// snip
// here the text from the text fields is read, but this only works the first time the buttonAction is called, the next times, it simply returns the first user input.
while a <= aantalNoten {
if let theLabel = view.viewWithTag(a) as? UITextField {
let tekstInput = theLabel.text!
userInput.append(tekstInput)
}
a += 1
}
// snip
setupLabels()
return
}
// snip
最佳答案
您有两个 ThirdViewController
实例,但您并不是故意的。
这个错误很明显:
2018-09-29 23:08:37.279170+0200 MyProject[57733:4748212] Warning: Attempt to present <MyProject.ThirdViewController: 0x7fc709125890> on <MyProject.SecondViewController: 0x7fc70900fcd0> whose view is not in the window hierarchy!
这告诉您 SecondViewController
正在尝试创建 ThirdViewController
,而 SecondViewController
甚至不在屏幕上。这表明错误出在 SecondViewController
中(可能是在不在屏幕上时观察通知或其他行为)。当然,您可能还有 SecondViewController
的两个实例。
我怀疑您正在尝试手动构建所有这些,而不是让 Storyboard 为您完成工作。这很好,但在那种情况下,这类错误更常见一些。进一步调试的最佳方法是设置一些断点并仔细检查对象的地址(例如 0x7fc709125890)。您将需要寻找要创建额外一个的位置。
关于ios - 为什么我的 iOS 应用程序中的函数会被调用两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52566139/