如何在Swift中设置UIBarButtonItem的操作 [英] How to set the action for a UIBarButtonItem in Swift

查看:760
本文介绍了如何在Swift中设置UIBarButtonItem的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置Swift中自定义UIBarButtonItem的操作?



以下代码成功地将按钮放在导航栏中:

  var b = UIBarButtonItem(标题:继续,样式:.Plain,target:self,action:nil)
self.navigationItem.rightBarButtonItem = b

现在,我想打电话给 func sayHello(){println触摸按钮时(你好)} 。到目前为止我的努力:

  var b = UIBarButtonItem(标题:继续,样式:.Plain,target:self,action :sayHello :) 
//还有`sayHello``shenHello()`和`sayHello():`

和..

  var b = UIBarButtonItem(标题:继续,样式:.Plain, target:self,action:@selector(sayHello :))
//还有`sayHello``sekeHello()`和`sayHello():`

和..

  var b = UIBarButtonItem(标题: 继续,风格:。平原,目标:自我,动作:@selector(self.sayHello :))
//也使用`self.sayHello``self.sayHello()`和`self.sayHello ():`

注意 sayHello()出现在intellisense中,但不起作用。



感谢您的帮助。



编辑:为后人,以下工作:

  var b = UIBarButtonItem(标题:继续,样式:。平面,目标:自我,行动:SA yHello)


解决方案

从Swift 2.2开始,有一个编译时检查选择器的特殊语法。它使用以下语法: #selector(methodName)



Swift 3及更高版本:

  var b = UIBarButtonItem(
title:Continue,
style:.plain,
target:self,
action:#selector(sayHello(sender :))


func sayHello(sender:UIBarButtonItem){
}

如果您不确定方法名称应该是什么样子,那么复制命令的特殊版本是很有帮助。将光标放在基本方法名称中(例如sayHello)并按 Shift + Control + Option + C 。这样就可以粘贴键盘上的符号名称。如果您还持有 Command ,它将复制包含该类型的合格符号名称。



Swift 2.3:

  var b = UIBarButtonItem(
title:Continue,
style :.普通,
目标:self,
操作:#selector(sayHello(_ :))


func sayHello(发件人:UIBarButtonItem){
}

这是因为在进行方法调用时,Swift 2.3中不需要第一个参数名。 / p>

您可以在此处了解有关swift.org语法的更多信息: https://swift.org/blog/swift-2-2-new-features/#compile-time-checked-selectors


How can the action for a custom UIBarButtonItem in Swift be set?

The following code successfully places the button in the navigation bar:

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:nil)
self.navigationItem.rightBarButtonItem = b

Now, I would like to call func sayHello() { println("Hello") } when the button is touched. My efforts so far:

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:sayHello:)
// also with `sayHello` `sayHello()`, and `sayHello():`

and..

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(sayHello:))
// also with `sayHello` `sayHello()`, and `sayHello():`

and..

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:@selector(self.sayHello:))
// also with `self.sayHello` `self.sayHello()`, and `self.sayHello():`

Note that sayHello() appears in the intellisense, but does not work.

Thanks for your help.

EDIT: For posterity, the following works:

var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:"sayHello")

解决方案

As of Swift 2.2, there is a special syntax for compiler-time checked selectors. It uses the syntax: #selector(methodName).

Swift 3 and later:

var b = UIBarButtonItem(
    title: "Continue",
    style: .plain,
    target: self,
    action: #selector(sayHello(sender:))
)

func sayHello(sender: UIBarButtonItem) {
}

If you are unsure what the method name should look like, there is a special version of the copy command that is very helpful. Put your cursor somewhere in the base method name (e.g. sayHello) and press Shift+Control+Option+C. That puts the ‘Symbol Name’ on your keyboard to be pasted. If you also hold Command it will copy the ‘Qualified Symbol Name’ which will include the type as well.

Swift 2.3:

var b = UIBarButtonItem(
    title: "Continue",
    style: .Plain,
    target: self,
    action: #selector(sayHello(_:))
)

func sayHello(sender: UIBarButtonItem) {
}

This is because the first parameter name is not required in Swift 2.3 when making a method call.

You can learn more about the syntax on swift.org here: https://swift.org/blog/swift-2-2-new-features/#compile-time-checked-selectors

这篇关于如何在Swift中设置UIBarButtonItem的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆