gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:14:48 25 4
gpt4 key购买 nike

我正在尝试以编程方式将右侧导航栏按钮项添加到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com