#UIBarButtonItem 上的选择器
#selector on UIBarButtonItem
我正在 Swift 2.2 中开发我的第一个 iOS 应用程序,我遇到了以下问题。
在实用程序 class 中,我有以下静态方法,由一些不同的 UIViewController
.
调用
static func setNavigationControllerStatusBar(myView: UIViewController, title: String, color: CIColor, style: UIBarStyle) {
let navigation = myView.navigationController!
navigation.navigationBar.barStyle = style
navigation.navigationBar.barTintColor = UIColor(CIColor: color)
navigation.navigationBar.translucent = false
navigation.navigationBar.tintColor = UIColor.whiteColor()
myView.navigationItem.title = title
let menuButton = UIBarButtonItem(image: UIImage(named: "menu"),
style: UIBarButtonItemStyle.Plain ,
target: self, action: #selector("Utils.menuClicked(_:)"))
myView.navigationItem.leftBarButtonItem = menuButton
}
func menuClicked(sender: UIButton!) {
// do stuff
}
我正在尝试通过一些不同的方式为这个按钮关联一个 #selector
,但是我总是遇到以下错误。
没有引号。
#selector(Utils.menuClicked(_:))
func menuClicked
应该在您的视图控制器中 class。但如果由于某种原因不是,你可以做
class Utils {
static let instance = Utils()
let menuButton = UIBarButtonItem(image: UIImage(named: "menu"),
style: UIBarButtonItemStyle.Plain ,
target: Utils.instance, action: #selector(Utils.menuClicked(_:)))
@objc func menuClicked(sender: UIBarButtonItem) {
// do stuff
}
}
Swift 2.2 deprecates using strings for selectors and instead introduces new syntax: #selector.
Using #selector will check your code at compile time to make sure the method you want to call actually exists. Even better, if the method doesn’t exist, you’ll get a compile error: Xcode will refuse to build your app, thus banishing to oblivion another possible source of bugs.
因此在 #selector
中删除您的方法的双引号。应该有用!
我正在 Swift 2.2 中开发我的第一个 iOS 应用程序,我遇到了以下问题。
在实用程序 class 中,我有以下静态方法,由一些不同的 UIViewController
.
static func setNavigationControllerStatusBar(myView: UIViewController, title: String, color: CIColor, style: UIBarStyle) {
let navigation = myView.navigationController!
navigation.navigationBar.barStyle = style
navigation.navigationBar.barTintColor = UIColor(CIColor: color)
navigation.navigationBar.translucent = false
navigation.navigationBar.tintColor = UIColor.whiteColor()
myView.navigationItem.title = title
let menuButton = UIBarButtonItem(image: UIImage(named: "menu"),
style: UIBarButtonItemStyle.Plain ,
target: self, action: #selector("Utils.menuClicked(_:)"))
myView.navigationItem.leftBarButtonItem = menuButton
}
func menuClicked(sender: UIButton!) {
// do stuff
}
我正在尝试通过一些不同的方式为这个按钮关联一个 #selector
,但是我总是遇到以下错误。
没有引号。
#selector(Utils.menuClicked(_:))
func menuClicked
应该在您的视图控制器中 class。但如果由于某种原因不是,你可以做
class Utils {
static let instance = Utils()
let menuButton = UIBarButtonItem(image: UIImage(named: "menu"),
style: UIBarButtonItemStyle.Plain ,
target: Utils.instance, action: #selector(Utils.menuClicked(_:)))
@objc func menuClicked(sender: UIBarButtonItem) {
// do stuff
}
}
Swift 2.2 deprecates using strings for selectors and instead introduces new syntax: #selector. Using #selector will check your code at compile time to make sure the method you want to call actually exists. Even better, if the method doesn’t exist, you’ll get a compile error: Xcode will refuse to build your app, thus banishing to oblivion another possible source of bugs.
因此在 #selector
中删除您的方法的双引号。应该有用!