Swift:自定义导航条图像,点击时不触发动作

时间:2023-01-16 16:38:39

I have a navigation bar on my viewcontroller.

视图控制器上有一个导航栏。

I have created a custom image (of a cog) and have got that to show right:

我创建了一个自定义的图像(一个齿轮),并得到了正确的显示:

//Add bar item
var image = UIImage(named: "settingsIcon")
image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

NavigationBar.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: nil, action: "addTapped")

I added the action to the above code:

我在上面的代码中添加了操作:

action: "addTapped"

Then I created a function:

然后我创建了一个函数:

func addTapped() {
    print("Tapped")
}

When I run the app, the image shows fine. When I click the image nothing happens, not even an error.

当我运行应用程序时,图像显示良好。当我点击图片时,什么也没有发生,甚至没有一个错误。

Am I doing something blatantly wrong here?

我做错什么了吗?

1 个解决方案

#1


1  

Try setting the target to self instead of nil.

尝试将目标设置为self而不是nil。

#1


1  

Try setting the target to self instead of nil.

尝试将目标设置为self而不是nil。