引用方法的 Objective-C 选择器
Referencing the Objective-C selector of a method
从Swift 2.2开始,以下代码给出了警告:
No method declared with Objective-C selector'sync'
if let tabBarController = segue.destinationViewController as? TabBarController {
tabBarController.navigationItem.rightBarButtonItem =
UIBarButtonItem(title: "Upload",
style: .Plain,
target: tabBarController,
action: "sync")
我应该用什么替换action: "sync"
它来消除警告?
我试过:
Selector("sync") // The Xcode provided fix which yields the same warning
#selector(tabBarController.sync()) // Error: Argument of '#selector' does not refer to initializer or method
Selector(tabBarController.sync()) // No error/warning but doesn't fire sync function
我认为您放错了操作函数 "sync"。将它保存在 TabBarController 中,因为您已将 TabBarController 的实例用作目标。下面的代码将起作用:
tabBarController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Upload", style: .Plain, target: tabBarController, action: "sync:")
在 TabBarController 中保留以下功能:
func sync(sender: AnyObject){
//your code here
}
希望它能解决您的问题。 :)
为了解决您的问题,请首先阅读 documentation 关于 Swift2.2 中的选择器的新内容。
示例:
使用 #selector(CLASS.sync)
而不是 Selector("sync")
。其中 CLASS 实际 class 包含此方法。
之所以这样做是因为:
The use of string literals for selector names is extremely
error-prone: there is no checking that the string is even a
well-formed selector, much less that it refers to any known method, or
a method of the intended class. Moreover, with the effort to perform
automatic renaming of Objective-C APIs, the link between Swift name
and Objective-C selector is non-obvious. By providing explicit "create
a selector" syntax based on the Swift name of a method, we eliminate
the need for developers to reason about the actual Objective-C
selectors being used.
从Swift 2.2开始,以下代码给出了警告:
No method declared with Objective-C selector'sync'
if let tabBarController = segue.destinationViewController as? TabBarController {
tabBarController.navigationItem.rightBarButtonItem =
UIBarButtonItem(title: "Upload",
style: .Plain,
target: tabBarController,
action: "sync")
我应该用什么替换action: "sync"
它来消除警告?
我试过:
Selector("sync") // The Xcode provided fix which yields the same warning
#selector(tabBarController.sync()) // Error: Argument of '#selector' does not refer to initializer or method
Selector(tabBarController.sync()) // No error/warning but doesn't fire sync function
我认为您放错了操作函数 "sync"。将它保存在 TabBarController 中,因为您已将 TabBarController 的实例用作目标。下面的代码将起作用:
tabBarController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Upload", style: .Plain, target: tabBarController, action: "sync:")
在 TabBarController 中保留以下功能:
func sync(sender: AnyObject){
//your code here
}
希望它能解决您的问题。 :)
为了解决您的问题,请首先阅读 documentation 关于 Swift2.2 中的选择器的新内容。
示例:
使用 #selector(CLASS.sync)
而不是 Selector("sync")
。其中 CLASS 实际 class 包含此方法。
之所以这样做是因为:
The use of string literals for selector names is extremely error-prone: there is no checking that the string is even a well-formed selector, much less that it refers to any known method, or a method of the intended class. Moreover, with the effort to perform automatic renaming of Objective-C APIs, the link between Swift name and Objective-C selector is non-obvious. By providing explicit "create a selector" syntax based on the Swift name of a method, we eliminate the need for developers to reason about the actual Objective-C selectors being used.