ios - 尝试添加 UINavigationBar 按钮时发生崩溃

标签 ios swift uinavigationbar uinavigationitem

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

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 上的一个方法。

关于ios - 尝试添加 UINavigationBar 按钮时发生崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45563614/

相关文章:

ios - UIWebView 在 Safari 中启动

iphone - 设置UITableView右侧显示的SectionIndex的宽度?

ios - 如何通过 Alamofire 递归获取树状结构的数据?

swift - 如何取消 iOS 11 中的透明导航栏?

ios - 与 NSTimer 一起使用时,进度条显示滴答动画而不是平滑动画

ios - 我是否必须升级到 Lion 才能在运行 iOS 5.1 的 iPad 上进行测试?

swift - 最小日期和最大日期之间的时差

ios - Swift - 带或不带图像的多部分/表单数据 POST 请求(上传)

ios - 在 Swift 2.0 中设置 UINavigationController 栏标题