简体   繁体   中英

Swift - dynamic array and changing the views

Let me begin explaining that I have just started working on my very first project. The project is a very simple buzzword like game, although with Celebrity names - users add celebrity names into the array, and then using start button, the celebrity label changes within the array.

As I am coding, I have one error: when I switch the views (from Add Celebrity, to Play), the whole array that was defined in the Add Celebrity disappears. I have investigated the problem, and it turns out, that whenever I change the view from the Add Celebrity view, the array disappears.

Here is my code:

@IBOutlet var addCelebrityText: UITextField!
@IBOutlet var numberOfPlayers: UITextField!
@IBOutlet var numberOfCelebrities: UITextField!
@IBOutlet var timeSet: UITextField!
@IBOutlet var timerField: UILabel!
@IBOutlet var celebField: UILabel!
@IBAction func scoreButton(sender: AnyObject) {

    println(celebrityName)
     }
@IBOutlet var startButtonField: UIButton!
@IBAction func startButton(sender: AnyObject) {
    shuffle(&celebrityName)
    celebField.text = "\(celebrityName[1])"
    startGame();


}
@IBAction func saveSettingsButton(sender: AnyObject) {
}
@IBAction func test(sender: AnyObject) {
    shuffle(&celebrityName)
    println(celebrityName)
}
@IBAction func addCelebrityButton(sender: AnyObject) {
    if addCelebrityText.text.isEmpty {

        let emptyAlert = UIAlertController(title: "No name celebrity", message: "You have to write down  name of the celebrity", preferredStyle: UIAlertControllerStyle.Alert)

        emptyAlert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))

        self.presentViewController(emptyAlert, animated: true, completion: nil)

    }

    else {

        let confirmAlert = UIAlertController(title: "Confirm", message: "Are you sure you want to add \(addCelebrityText.text) into the game?" , preferredStyle: .Alert)

        confirmAlert.addAction(UIAlertAction(title:"Yes", style: UIAlertActionStyle.Default, handler: {(confirmAction)-> Void in self.addCelebrity(); println("\(self.addCelebrityText.text) added into the database")}))

        confirmAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))


        presentViewController(confirmAlert, animated: true, completion: nil)




    }

}

var celebrityName = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    self.view.endEditing(true)
}

func addCelebrity() {

    celebrityName.append(addCelebrityText.text)

}



var startTime = NSTimeInterval()
var timer = NSTimer()
var gameTime:Double = 60




func startGame() {
    startButtonField.setTitle("PAUSE", forState: .Normal)
    let aSelector : Selector = "updateTime"
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
    startTime = NSDate.timeIntervalSinceReferenceDate()

}

func updateTime() {
    var currentTime = NSDate.timeIntervalSinceReferenceDate()
    var elapsedTime = currentTime - startTime
    var seconds = gameTime-elapsedTime
    if seconds > 0 {
        elapsedTime -= NSTimeInterval(seconds)
        println("\(Int(seconds))")
        timerField.text = "\(Int(seconds))"
    } else {
        timer.invalidate()
        startButtonField.setTitle("START", forState: .Normal)
    }
}

func shuffle<C: MutableCollectionType where C.Index == Int>(inout list: C) {
    let count = countElements(list)
    for i in 0..<(count - 1) {
        let j = Int(arc4random_uniform(UInt32(count - i))) + i
        swap(&list[i], &list[j])
    }
}
}

您最好使用单例类声明的数组,以便保留数组中的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM