bwc*_*ley 0 parameters ibaction swift
我目前有一个函数从数据库收集时间并返回它以供其他函数使用.它需要一个参数,该参数存储在应用程序的另一部分中,以便从数据库中收集值.
当我想在IBAction函数中调用此函数时,我的问题出现了.
这是我的函数代码:
func getDBValue(place: GMSPlace) -> Int {
var expectedValue = 0
databaseRef.child("values").child(place.placeID).observe(.value, with: { (snapshot) in
let currentValue = snapshot.value as? [Int]
if currentValue == nil {
self.noValue()
expectedValue = 0
} else {
let sumValue = currentValue?.reduce(0, +)
let avgValue = sumValue! / (currentValue?.count)!
print("The current value is \(String(describing: avgValue))")
expectedValue = avgValue
self.valueLabel.text = String(describing: avgValue)
}
})
print("This is the expected WT: \(expectedWaitTime)")
return expectedValue
}
Run Code Online (Sandbox Code Playgroud)
这是我的IBAction函数的代码,它有多个参数的问题:
@IBAction func addValuePressed(_ sender: Any, place: GMSPlace) {
print("This is the place ID: \(place.placeID)")
var expectedValue = getDBValue(place: place)
expectedValue = expectedValue + 1
print("The expectedValue is now: \(expectedValue)")
self.valueLabel.text = String(describing: expectedValue)
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)错误.经过一些测试,似乎错误是由place: GMSPlace我的IBAction函数中添加的参数引起的.有关如何解决此问题的任何想法?
IBAction方法不能有任意签名.您无法在此处添加额外参数.有没有办法让按钮发送你这(如何按钮知道什么place是?)通常,这被处理或者通过有仅是一个UI元素指向这个动作(所以你知道按下哪个按钮),或使用tag上发件人识别它.每个视图都有一个tag只是整数的属性.您可以在Interface Builder或代码中进行设置,然后您可以读取它以识别发件人.
首先阅读文档中的目标操作,解释它如何在各种平台上运行.一般来说,一个签名IBAction必须是:
@IBAction func action(_ sender: Any)
Run Code Online (Sandbox Code Playgroud)
但是,在iOS上,它也可能是:
@IBAction func action(_ sender: Any, forEvent: UIEvent)
Run Code Online (Sandbox Code Playgroud)
正如泰勒M在下面指出的那样,你也可以使用这个签名(虽然我不能记得,如果这在iOS之外有效;我只是亲自在那里使用它).
@IBAction func action()
Run Code Online (Sandbox Code Playgroud)
但就是这样.没有其他允许的签名.