我正在尝试以编程方式将右侧导航栏按钮项添加到视图中,我使用的代码如下:
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: Selector(("buttonTapped:")))
self.navigationItem.rightBarButtonItem = button
响应添加的右侧栏按钮的响应功能如下:
func buttonTapped() {
print("Right Bar button action")
}
当我尝试运行项目时,新添加的右侧栏按钮出现在导航栏上。但是,当我点击它时,出现以下错误,应用程序崩溃。
2017-08-08 12:37:26.641 Grow[14229:3786869] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Grow.DashBoardViewControllerMain rightButtonAction:]: unrecognized selector sent to instance 0x7fd719c12700'
*** First throw call stack:
(
0 CoreFoundation 0x00000001101c2b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000011317c141 objc_exception_throw + 48
2 CoreFoundation 0x0000000110232134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000110149840 ___forwarding___ + 1024
4 CoreFoundation 0x00000001101493b8 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001113e4d82 -[UIApplication sendAction:to:from:forEvent:] + 83
6 UIKit 0x000000011181f917 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 149
7 UIKit 0x00000001113e4d82 -[UIApplication sendAction:to:from:forEvent:] + 83
8 UIKit 0x00000001115695ac -[UIControl sendAction:to:forEvent:] + 67
9 UIKit 0x00000001115698c7 -[UIControl _sendActionsForEvents:withEvent:] + 450
10 UIKit 0x0000000111569a3b -[UIControl _sendActionsForEvents:withEvent:] + 822
11 UIKit 0x0000000111568802 -[UIControl touchesEnded:withEvent:] + 618
12 UIKit 0x00000001114527ea -[UIWindow _sendTouchesForEvent:] + 2707
13 UIKit 0x0000000111453f00 -[UIWindow sendEvent:] + 4114
14 UIKit 0x0000000111400a84 -[UIApplication sendEvent:] + 352
15 UIKit 0x0000000111be45d4 __dispatchPreprocessedEventFromEventQueue + 2926
16 UIKit 0x0000000111bdc532 __handleEventQueue + 1122
17 CoreFoundation 0x0000000110168c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18 CoreFoundation 0x000000011014e0cf __CFRunLoopDoSources0 + 527
19 CoreFoundation 0x000000011014d5ff __CFRunLoopRun + 911
20 CoreFoundation 0x000000011014d016 CFRunLoopRunSpecific + 406
21 GraphicsServices 0x000000011646ca24 GSEventRunModal + 62
22 UIKit 0x00000001113e3134 UIApplicationMain + 159
23 Grow 0x000000010f32f5a7 main + 55
24 libdyld.dylib 0x0000000114e3665d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
有人可以帮我解决问题吗?
答案 0 :(得分:0)
看看这一行:
action: Selector(("buttonTapped:"))
如果引用名为buttonTapped:
的选择器。
但是,您的函数名为buttonTapped
,没有参数。相应地修改您的选择器,因此它将采用UIBarButtonItem
参数。
另外请注意,您应该使用#selector(functionName)
,因为它有实时的名称检查功能,可能会警告您缺少功能。
let button = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(buttonTapped(_:))))
self.navigationItem.rightBarButtonItem = button
func buttonTapped(_ sender: UIBarButtonItem) {
print("Right bar button action")
}
答案 1 :(得分:0)
代码
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: Selector(("buttonTapped")))
self.navigationItem.rightBarButtonItem = button
func buttonTapped() {
print("Right Bar button action")
}
崩溃报告未找到操作rightButtonAction
:.
请重新检查并替换rightButtonAction
:buttonTapped
,其中buttonTapped
是self
上的方法。
答案 2 :(得分:0)
你的选择器需要一个带参数的函数。虽然你的功能没有。
如下所示,考虑使用 Swift 2 来编写函数。
func buttonTapped(sender: UIBarButtonItem){
print("Right Bar button action")
}
如果使用 swift 3 ,请编写选择,如下所示
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: #selector(Your_View_Controller.buttonTapped(_ :)))
和写功能,如
func buttonTapped(_ sender: UIBarButtonItem){
print("Right Bar button action")
}
答案 3 :(得分:0)
<script>
var btcPrice;
function UpdateBtcPrice(){
$.ajax({
type: "GET",
url: "https://api.coinmarketcap.com/v1/ticker/bitcoin/",
dataType: "json",
success: function(result){
btcPrice = result[0].price_usd;
},
error: function(err){
console.log(err);
}
});
}
</script>
答案 4 :(得分:0)
您的buttonTapped
功能没有采用任何参数。所以你不需要使用:
(冒号)
尝试删除它并查看它是否有效。
答案 5 :(得分:0)
您可以使用以下内容:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Button", style: .plain, target: self, action: #selector(buttonTapped))
而不是这两行:
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: Selector(("buttonTapped:")))
self.navigationItem.rightBarButtonItem = button
答案 6 :(得分:0)
发生错误是因为您在选择器文本中设置了':'。这意味着你在函数(发送者)中等待参数,但你的函数没有参数。 (并且在运行时系统中找不到功能)您可以尝试删除':'符号或在函数中添加sender参数。我想写这样的东西
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: #selector(buttonTapped))
self.navigationItem.rightBarButtonItem = button
功能:
func buttonTapped(_ sender: UIBarButtonItem) {
print("Right Bar button action")
}