gpt4 book ai didi

ios - 为什么 viewDidLoad() 被调用两次?

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:38 25 4
gpt4 key购买 nike

出于某种原因,我的数据被两次保存到模型中。为什么 viewdidload 运行两次?这个 Controller 的目的是成为记分牌。它由使用核心数据的数据填充。我尝试删除 tablecontroller 并添加一个新的。

import UIKit
class ScoreboardTableViewController: UITableViewController {

var model = scoreboardModel.sharedInstance
var numbers = [9,12,9]// [Int]()
var timer = "k"
var date = "thedateToday"



override func viewDidLoad() {
super.viewDidLoad()
var count = numbers.count
var x = String(count)


if(timer != "k"){
model.saveScoreboard(date, numsMemorized: x,time: timer)
println("saved!")
}
model.getScoreboards()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

// MARK: - Table view data source


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return model.scoreboard.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("Cellll", forIndexPath: indexPath) as UITableViewCell

let score = model.getScore(indexPath)
cell.textLabel!.text = ("\(score.numsMemorized) | \(score.time)")

return cell
}

}

连接到 ScoreboardTableViewController 的 View 是:

import UIKit

class PracticeReciteController: UIViewController, UITextFieldDelegate {

var theTime = ""

@IBOutlet weak var howManyCorrect: UILabel!

var numbers = [9,12,9]
var x = 0

@IBOutlet weak var textField: UITextField!

var howManyCorrectNum = 0

override func viewDidLoad() {
super.viewDidLoad()



var outof = numbers.count
howManyCorrect.text = "\(howManyCorrectNum) / \(outof)"

textField.delegate = self;
textField.addTarget(self, action:"edited", forControlEvents:UIControlEvents.EditingChanged)

}

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



func edited() {

//[todo] if on last number in array & correct. endgame/collectscore
var userInput = textField.text.toInt()

if numbers[x] == userInput{

if x+1 == numbers.count{


performSegueWithIdentifier("scoreboardIdentifier", sender: self)

}
//println("correct")
x++

howManyCorrect.text = String(x) + "/" + String(numbers.count)

//I set here timer so if user enter correct input then it will remove text after some time
var timer = NSTimer.scheduledTimerWithTimeInterval(0.8, target: self, selector: Selector("resetText"), userInfo: nil, repeats: false)
}else if userInput?.isDouble() == numbers[x].isDouble(){
println("incorrect")
}
else{
println("huh?")

}
}
//This method will call after some time use if user enter correct input
func resetText(){
textField.text = ""
}




override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {


var DestViewController = segue.destinationViewController as ScoreboardTableViewController

DestViewController.numbers = numbers
DestViewController.timer = theTime


}

}

最佳答案

这个问题之前已经回答过很多次了: viewDidLoad getting called twice on rootViewController at launch

viewDidLoad is called twice

无论如何,给你一个重要的建议:不要在 viewDidLoad 中放置任何会破坏你的应用程序/数据的代码。 viewDidLoad 不假定在 ViewController 的生命周期中只调用一次

关于ios - 为什么 viewDidLoad() 被调用两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32796362/

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