我一直在尝试使用swift在Xcode的firebase引用中获取最后一个值,然后将这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)
})
}
最佳答案
假设甲板1-甲板10已定义为出口,请尝试
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]
}
也许可以使用一些循环来代替
关于ios - 抓取最后的十个firebase值并将它们分配给十个UIButton的标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37516636/