尝试添加 UI 导航栏按钮时发生崩溃



我正在尝试以编程方式将右侧导航栏按钮项添加到视图中,我使用的代码如下:

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

有人可以帮助我找出问题吗?

在代码中

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其中buttonTappedself上的一种方法。

你的选择器需要一个有参数的函数。虽然你的函数没有这个。

考虑到您使用的是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")
}
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self, action: #selector(buttonTapped(button:)))
self.navigationItem.rightBarButtonItem = button
func buttonTapped(button: UIBarButtonItem) {
print("bar button taped")
}

您的buttonTapped函数不采用任何参数。所以你不需要使用:(冒号(

尝试删除它,看看它是否有效。

您可以使用以下内容:

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

请看这一行:

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")
}

由于在选择器文本中设置了":"而发生错误。这意味着您在函数(发送者(中等待参数,但您的函数没有参数。(并且在运行时系统中找不到功能(您可以尝试删除":"符号或在函数中添加发送方参数。我想写这样的东西

let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add,     target: self, action:  #selector(buttonTapped))
self.navigationItem.rightBarButtonItem = button

该函数:

func buttonTapped(_ sender: UIBarButtonItem) {
print("Right Bar button action")
}

最新更新