【发布时间】:2016-02-16 11:38:24
【问题描述】:
在 iOS 8 中,我的以下功能按预期工作:
func showConfirmBox(msg:String, title:String,
firstBtnStr:String,
secondBtnStr:String,
caller:UIViewController) {
let userPopUp = UIAlertController(title:title,
message:msg, preferredStyle:UIAlertControllerStyle.Alert)
userPopUp.addAction(UIAlertAction(title:firstBtnStr, style:UIAlertActionStyle.Default,
handler:{action in}))
userPopUp.addAction(UIAlertAction(title:secondBtnStr, style:UIAlertActionStyle.Default,
handler:{action in}))
caller.presentViewController(userPopUp, animated: true, completion: nil)
}
我想做如下的事情,以便将在触摸一个或另一个按钮时要执行的方法作为参数传递:
func showConfirmBox(msg:String, title:String,
firstBtnStr:String, firstSelector:Selector,
secondBtnStr:String, secondSelector:Selector,
caller:UIViewController) {
let userPopUp = UIAlertController(title:title,
message:msg, preferredStyle:UIAlertControllerStyle.Alert)
userPopUp.addAction(UIAlertAction(title:firstBtnStr, style:UIAlertActionStyle.Default,
handler:{action in caller.firstSelector()}))
userPopUp.addAction(UIAlertAction(title:secondBtnStr, style:UIAlertActionStyle.Default,
handler:{action in caller.secondSelector()}))
caller.presentViewController(userPopUp, animated: true, completion: nil)
}
显然,我对 firstSelector 和 secondSelector 的操作不正确,因为我到目前为止所尝试的方法都不起作用。我想我没有使用正确的语法来做我想做的事,但我确信可以做我想做的事。知道正确执行此操作的方法吗?
【问题讨论】:
-
“没有工作”是什么意思?请提供更具体的信息。
-
我的意思是我从编译器收到错误消息。如果有用,我可以包括那些。而不是我认为我的第二个函数中的语法是错误的。
-
我正在尝试自己寻找其他方法(例如使用泛型),但此时仍然没有成功。
-
现在可以了,我想通了。 Just : 使用函数名并且没有引号。谢谢!
标签: ios swift function parameter-passing