我正在使用Swift 2和Xcode 7.今天早上,我的应用程序运行正常。但是,并非不是。在我的viewDidLoad
方法中,我print("viewdidload")
。在我的viewDidAppear
方法中,我print("viewdidappear
)。 Viewdidload打印到日志,但viewdid出现不是。我的应用程序停留在启动屏幕上。这是我的完整代码:
//
// ViewController.swift
// Math Practice
//
// Created by Pranav Wadhwa on 8/7/15.
// Copyright (c) 2015 Pranav Wadhwa. All rights reserved.
//
import UIKit
import iAd
class ViewController: UIViewController, ADBannerViewDelegate {
@IBOutlet var iAdBannerView: ADBannerView?
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.iAdBannerView?.hidden = false
}
@IBOutlet var startStop: UIButton!
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
seconds = 0
minutes = 0
correct = 0
startStop.setTitle("Start", forState: .Normal)
total = 0
correctLabel.text = "0/0"
timerLabel.text = "00:00"
questionLabel.text = ""
button1.setTitle("", forState: .Normal)
button2.setTitle("", forState: .Normal)
button3.setTitle("", forState: .Normal)
button4.setTitle("", forState: .Normal)
timer.invalidate()
already = false
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
self.iAdBannerView?.hidden = true
}
var firstNumber = Int()
var secondNumber = Int()
var sign = String()
var arrayOfSigns = ["add", "subtract", "divide", "multiply"]
var correctAnswer = Int()
var buttonWithCorrectAnswer = Int()
var correct = Int()
var total = Int()
@IBOutlet var timerLabel: UILabel!
@IBOutlet var questionLabel: UILabel!
@IBOutlet var correctLabel: UILabel!
@IBOutlet var button1: UIButton!
@IBOutlet var button2: UIButton!
@IBOutlet var button3: UIButton!
@IBOutlet var button4: UIButton!
var timer = NSTimer()
func updateTime() {
seconds += 01
if seconds == 60 {
minutes++
seconds = 0
}
if seconds < 10 {
timerLabel.text = "\(minutes):0\(seconds)"
} else {
timerLabel.text = "\(minutes):\(seconds)"
}
}
@IBAction func start(sender: AnyObject) {
if already == false {
createQuestion()
startStop.setTitle("Pause", forState: .Normal)
already = true
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
userLevel.text = ""
userImage.hidden = true
} else {
userImage.hidden = false
startStop.setTitle("Start", forState: .Normal)
questionLabel.text = ""
button1.setTitle("", forState: .Normal)
button2.setTitle("", forState: .Normal)
button3.setTitle("", forState: .Normal)
button4.setTitle("", forState: .Normal)
timer.invalidate()
already = false
}
}
@IBAction func reset(sender: AnyObject) {
if NSUserDefaults.standardUserDefaults().objectForKey("correctQuestions") != nil {
totalCorrectQuestions = NSUserDefaults.standardUserDefaults().objectForKey("correctQuestions") as! Int
}
userLevel.text = "Level " + String(usersLevel)
userImage.hidden = false
seconds = 0
minutes = 0
correct = 0
total = 0
correctLabel.text = "0/0"
timerLabel.text = "00:00"
questionLabel.text = ""
button1.setTitle("", forState: .Normal)
button2.setTitle("", forState: .Normal)
button3.setTitle("", forState: .Normal)
button4.setTitle("", forState: .Normal)
timer.invalidate()
already = false
}
var seconds = 0
var minutes = 0
var already = false
@IBAction func button1(sender: AnyObject) {
if buttonWithCorrectAnswer == 1 {
correct += 1
}
total += 1
createQuestion()
}
@IBAction func button2(sender: AnyObject) {
if buttonWithCorrectAnswer == 2 {
correct += 1
}
total += 1
createQuestion()
}
@IBAction func button3(sender: AnyObject) {
if buttonWithCorrectAnswer == 3 {
correct += 1
}
total += 1
createQuestion()
}
@IBAction func button4(sender: AnyObject) {
if buttonWithCorrectAnswer == 4 {
correct += 1
}
total += 1
createQuestion()
}
func createQuestion() {
correctLabel.text = "\(correct)/\(total)"
firstNumber = Int(arc4random_uniform(39))
secondNumber = Int(arc4random_uniform(39))
firstNumber -= 20
secondNumber -= 20
let i = Int(arc4random_uniform(4))
sign = arrayOfSigns[i]
if sign == "divide" {
firstNumber = Int(arc4random_uniform(39))
secondNumber = Int(arc4random_uniform(39))
firstNumber -= 20
secondNumber -= 20
}
if sign == "add" {
questionLabel.text = "\(firstNumber) + \(secondNumber)"
correctAnswer = firstNumber + secondNumber
}
if sign == "subtract" {
questionLabel.text = "\(firstNumber) - \(secondNumber)"
correctAnswer = firstNumber - secondNumber
}
if sign == "divide" {
firstNumber = firstNumber * secondNumber
questionLabel.text = "\(firstNumber) / \(secondNumber)"
correctAnswer = firstNumber / secondNumber
}
if sign == "multiply" {
questionLabel.text = "\(firstNumber) * \(secondNumber)"
correctAnswer = firstNumber * secondNumber
}
createAnswers()
}
var firstRandomAnswer = Int()
var secondRandomAnswer = Int()
var thirdRandomAnswer = Int()
var fourthRandomAnswer = Int()
func createAnswers() {
buttonWithCorrectAnswer = Int(arc4random_uniform(4) + 1)
//Insert Correct Answer
if buttonWithCorrectAnswer == 1 {
button1.setTitle("\(correctAnswer)", forState: .Normal)
} else if buttonWithCorrectAnswer == 2 {
button2.setTitle("\(correctAnswer)", forState: .Normal)
} else if buttonWithCorrectAnswer == 3 {
button3.setTitle("\(correctAnswer)", forState: .Normal)
} else if buttonWithCorrectAnswer == 4 {
button4.setTitle("\(correctAnswer)", forState: .Normal)
}
//Setup Fake Answer
if sign == "add" || sign == "subtract" || sign == "divide" {
firstRandomAnswer = Int(arc4random_uniform(79))
firstRandomAnswer -= 40
secondRandomAnswer = Int(arc4random_uniform(79))
secondRandomAnswer -= 40
while secondRandomAnswer == firstRandomAnswer {
secondRandomAnswer = Int(arc4random_uniform(79))
secondRandomAnswer -= 40
}
thirdRandomAnswer = Int(arc4random_uniform(79))
thirdRandomAnswer -= 40
while thirdRandomAnswer == secondRandomAnswer || thirdRandomAnswer == firstRandomAnswer {
thirdRandomAnswer = Int(arc4random_uniform(79))
thirdRandomAnswer -= 40
}
fourthRandomAnswer = Int(arc4random_uniform(79))
fourthRandomAnswer -= 40
while fourthRandomAnswer == thirdRandomAnswer || fourthRandomAnswer == secondRandomAnswer || fourthRandomAnswer == firstRandomAnswer {
fourthRandomAnswer = Int(arc4random_uniform(79))
fourthRandomAnswer -= 40
}
}
if sign == "multiply" {
firstRandomAnswer = Int(arc4random_uniform(799))
firstRandomAnswer -= 400
secondRandomAnswer = Int(arc4random_uniform(799))
secondRandomAnswer -= 400
while secondRandomAnswer == firstRandomAnswer {
secondRandomAnswer = Int(arc4random_uniform(799))
secondRandomAnswer -= 400
}
thirdRandomAnswer = Int(arc4random_uniform(799))
thirdRandomAnswer -= 400
while thirdRandomAnswer == secondRandomAnswer || thirdRandomAnswer == firstRandomAnswer {
thirdRandomAnswer = Int(arc4random_uniform(799))
thirdRandomAnswer -= 400
}
fourthRandomAnswer = Int(arc4random_uniform(799))
fourthRandomAnswer -= 400
while fourthRandomAnswer == thirdRandomAnswer || fourthRandomAnswer == secondRandomAnswer || fourthRandomAnswer == firstRandomAnswer {
fourthRandomAnswer = Int(arc4random_uniform(799))
fourthRandomAnswer -= 400
}
}
//Insert Fake Answers
if buttonWithCorrectAnswer != 1 {
button1.setTitle("\(firstRandomAnswer)", forState: .Normal)
}
if buttonWithCorrectAnswer != 2 {
button2.setTitle("\(secondRandomAnswer)", forState: .Normal)
}
if buttonWithCorrectAnswer != 3 {
button3.setTitle("\(thirdRandomAnswer)", forState: .Normal)
}
if buttonWithCorrectAnswer != 4 {
button4.setTitle("\(fourthRandomAnswer)", forState: .Normal)
}
}
@IBOutlet var backgroundImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
super.viewDidAppear(false)
print("viewdidload")
correctLabel.text = "0/0"
timerLabel.text = "00:00"
self.canDisplayBannerAds = true
self.iAdBannerView?.delegate = self
self.iAdBannerView?.hidden = true
var image = arc4random_uniform(2)
image++
userImage.hidden = false
backgroundImage.image = UIImage(named: "background_" + String(image))
if NSUserDefaults.standardUserDefaults().objectForKey("correctQuestions") != nil {
totalCorrectQuestions = NSUserDefaults.standardUserDefaults().objectForKey("correctQuestions") as! Int
}
totalCorrectQuestions += correct
NSUserDefaults.standardUserDefaults().setObject(totalCorrectQuestions, forKey: "correctQuestions")
if NSUserDefaults.standardUserDefaults().objectForKey("userLevel") != nil {
usersLevel = NSUserDefaults.standardUserDefaults().objectForKey("userLevel") as! Int
}
NSUserDefaults.standardUserDefaults().setObject(usersLevel, forKey: "userLevel")
userLevel.text = "Level " + String(usersLevel)
pointsForUpgrade = usersLevel * 10
for var i = 1; i > 0; i++ {
totalCorrectQuestions += correct
NSUserDefaults.standardUserDefaults().setObject(totalCorrectQuestions, forKey: "correctQuestions")
if NSUserDefaults.standardUserDefaults().objectForKey("userLevel") != nil {
usersLevel = NSUserDefaults.standardUserDefaults().objectForKey("userLevel") as! Int
}
if totalCorrectQuestions >= pointsForUpgrade {
usersLevel++
pointsForUpgrade = usersLevel * 10
}
NSUserDefaults.standardUserDefaults().setObject(usersLevel, forKey: "userLevel")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
var pointsForUpgrade = Int()
@IBOutlet var userImage: UIImageView!
var totalCorrectQuestions = Int()
var usersLevel = 1
@IBOutlet var userLevel: UILabel!
}
我在哪里犯错误?
答案 0 :(得分:0)
代码无效的原因是因为我试图在viewDidLoad方法中永久地使用循环。我需要删除它。