【发布时间】:2014-12-10 11:16:14
【问题描述】:
让我开始解释我刚刚开始从事我的第一个项目。该项目是一个非常简单的流行词类游戏,虽然有名人姓名 - 用户将名人姓名添加到数组中,然后使用开始按钮,名人标签会在数组中发生变化。
在编写代码时,我遇到了一个错误:当我切换视图(从添加名人到播放)时,在添加名人中定义的整个数组都消失了。我调查了这个问题,结果发现,每当我从 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])
}
}
}
【问题讨论】:
标签: ios arrays swift dynamic views