Swift - 动态数组和更改视图

时间:2014-12-10 11:16:15

标签: ios arrays swift dynamic views

让我开始解释我刚刚开始研究我的第一个项目。该项目是一个非常简单的流行语,如游戏,虽然有名人的名字 - 用户将名人姓名添加到数组中,然后使用开始按钮,名人标签在数组内发生变化。

正如我正在编码,我有一个错误:当我切换视图(从添加名人,到播放)时,添加名人中定义的整个数组都会消失。我已经调查了这个问题,事实证明,每当我从Add Celebrity视图更改视图时,数组都会消失。

这是我的代码:

@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])
    }
}
}

1 个答案:

答案 0 :(得分:0)

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