我一直试图使用swift在Xcode中的firebase引用中获取最后的值,然后将这10个值分配给10个不同的UIButton标题。我尝试了很多东西,但大部分时间都没有发生任何事情,或者我遇到了崩溃。我已经尝试过断点,但因为代码太小而且没有运行时错误或警告,它更多地与我如何做(概念)有关。任何建议都会很棒,我希望这可以帮助他们在早期iOS和firebase时代的未来观点。非常感谢你的时间和错误!
以下是我在视频加载时触发崩溃的最新代码:
// import UIKit (already imported the module)
// UIButtons deck1 - deck10 are defined
// firebase reference refName is created
override func viewDidLoad() {
super.viewDidLoad()
var titleArray = [String]()
var buttonArray : [UIButton:String] = [ // creating a dictionary to make a link from UIButtons to a String that will contain their title
deck1 : "",
deck2 : "",
deck3 : "",
deck4 : "",
deck5 : "",
deck6 : "",
deck7 : "",
deck8 : "",
deck9 : "",
deck10 : ""
]
refName.queryLimitedToLast(10).observeEventType(.Value, withBlock: { snapshot in // getting last
for child in snapshot.children { // getting the last ten values
let title = child.value as String // assigning each value to a variable
titleArray.append(title) // adding each variable to an array of values as strings
}
buttonArray[self.deck1] = titleArray[0] // assigning each value that is a string in the array 'titleArray' to a UIButton key
buttonArray[self.deck2] = titleArray[1]
buttonArray[self.deck3] = titleArray[2]
buttonArray[self.deck4] = titleArray[3]
buttonArray[self.deck5] = titleArray[4]
buttonArray[self.deck6] = titleArray[5]
buttonArray[self.deck7] = titleArray[6]
buttonArray[self.deck8] = titleArray[7]
buttonArray[self.deck9] = titleArray[8]
buttonArray[self.deck10] = titleArray[9]
self.deck1.setTitle(buttonArray[self.deck1], forState: UIControlState.Normal)
self.deck2.setTitle(buttonArray[self.deck2], forState: UIControlState.Normal)
self.deck3.setTitle(buttonArray[self.deck3], forState: UIControlState.Normal)
self.deck4.setTitle(buttonArray[self.deck4], forState: UIControlState.Normal)
self.deck5.setTitle(buttonArray[self.deck5], forState: UIControlState.Normal)
self.deck6.setTitle(buttonArray[self.deck6], forState: UIControlState.Normal)
self.deck7.setTitle(buttonArray[self.deck7], forState: UIControlState.Normal)
self.deck8.setTitle(buttonArray[self.deck8], forState: UIControlState.Normal)
self.deck9.setTitle(buttonArray[self.deck9], forState: UIControlState.Normal)
self.deck10.setTitle(buttonArray[self.deck10], forState: UIControlState.Normal)
})
}
答案 0 :(得分:0)
假设已经将deck1-deck10定义为出口,请尝试
deck1.titleLabel!.text = titleArray[0]
deck2.titleLabel!.text = titleArray[1]
....
所以,
// import UIKit (already imported the module)
// UIButtons deck1 - deck10 are defined
// firebase reference refName is created
override func viewDidLoad() {
super.viewDidLoad()
var titleArray = [String]()
refName.queryLimitedToLast(10).observeEventType(.Value, withBlock: { snapshot in // getting last
for child in snapshot.children { // getting the last ten values
let title = child.value as String // assigning each value to a variable
titleArray.append(title) // adding each variable to an array of values as strings
}
}
}
override func viewDidAppear(animated: Bool) {
deck1.titleLabel!.text = titleArray[0] // assigning each value that is a string in the array 'titleArray' to a UIButton key
deck2.titleLabel!.text = titleArray[1]
deck3.titleLabel!.text = titleArray[2]
deck4.titleLabel!.text = titleArray[3]
deck5.titleLabel!.text = titleArray[4]
deck6.titleLabel!.text = titleArray[5]
deck7.titleLabel!.text = titleArray[6]
deck8.titleLabel!.text = titleArray[7]
deck9.titleLabel!.text = titleArray[8]
deck10.titleLabel!.text = titleArray[9]
}
也许可以使用一些循环