Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 行动不起作用_Ios_Swift_Uibarbuttonitem - Fatal编程技术网

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")