Ios 行动不起作用
上述代码嵌入在地图导航控制器中。该操作无效,您知道为什么(我没有看到控制台中的“显示”)您将Ios 行动不起作用,ios,swift,uibarbuttonitem,Ios,Swift,Uibarbuttonitem,上述代码嵌入在地图导航控制器中。该操作无效,您知道为什么(我没有看到控制台中的“显示”)您将nil作为选择器的目标。在这种情况下,它应该是self: func showFilterPanel() { println("Showing") } let backButton = UIBarButtonItem(title: "Horse", style: UIBarButtonItemStyle.Plain, target: nil, action: "showFilterPanel")
nil
作为选择器的目标。在这种情况下,它应该是self
:
func showFilterPanel() {
println("Showing")
}
let backButton = UIBarButtonItem(title: "Horse", style: UIBarButtonItemStyle.Plain, target: nil, action: "showFilterPanel")
@IBAction func tapGesture(sender: AnyObject) {
self.navigationItem.leftBarButtonItem = self.backButton
}
您还需要重新安排它,将UIBarButtonItem的实例化向下移动到方法范围,以便能够在其实例化中引用
self
。当您在Swift中创建属性时,您不能在其创建过程中引用其他属性或self
,除非它是一个计算或延迟实例化的属性。因此正确的代码应该是:let backButton=uibarbuttoneim(标题:“Horse”,样式:uibarbuttonimstyle.Plain,目标:self,操作:“showFilterPanel”)
let backButton = UIBarButtonItem(title: "Horse", style: UIBarButtonItemStyle.Plain, target: nil, action: "showFilterPanel")