[英]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])
}
}
}
您最好使用单例类声明的数组,以便保留数组中的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.