崩溃试图添加UINavigationBar的按钮时出现
我想一个右侧导航栏按钮项添加到视图编程和我使用的代码如下:崩溃试图添加UINavigationBar的按钮时出现
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
有人可以帮我找出问题吗?
看这句话:
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")
}
我已经修改了问题,因为有一些输入错误......请有再看一遍.. –
@JobinsJohn我修改了我的答案。 – the4kman
在代码
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
上的方法。
您的选择器需要一个具有参数的函数。虽然你的功能没有。
考虑到您正在使用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")
}
请添加一条触发错误的描述并解决它。 – clemens
你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
错误的发生是因为你设置 ':' 在选择文本。这意味着你在函数(发送者)中等待参数,但是你的函数没有参数。 (并且在运行时系统中找不到函数)您可以尝试删除':'符号或在函数中添加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")
}
#selector( “buttonTapped:”) –
冒号 “:” 使用时存在的功能额外PARAM。但是你的功能没有。 –
设按钮=的UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.add,目标:自我,动作:#selector(buttonTapped)) self.navigationItem.rightBarButtonItem =按钮 –