问题
I'm trying to create multiple buttons with a for loop, but I'm having problems using the (sender:) function.
I have the following code
func setUpButtons(){
for i in 1...3 {
let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 100, width: 75, height: 100))
btn.center = CGPoint(x: 20 + 100.0 * CGFloat(i), y: 200)
btn.backgroundColor = UIColor.green
btn.setTitle("Click Me", for: UIControlState.normal)
btn.addTarget(self, action: Selector(("buttonAction:")), for: UIControlEvents.touchUpInside)
btn.tag = i
self.view.addSubview(btn)
}
}
func buttonAction(sender: UIButton!) {
let btnsendtag: UIButton = sender
if btnsendtag.tag == 1 {
print("Button 1")
} else if btnsendtag.tag == 2 {
print("Button 2")
} else if btnsendtag.tag == 3 {
print("Button 3")
}
}
When I press a button it shows the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Learn_ABCs.ViewController buttonAction:]: unrecognized selector sent to instance 0x7f9789d0bc50'
*** First throw call stack:
(
0 CoreFoundation 0x00000001095ac34b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001061fd21e objc_exception_throw + 48
2 CoreFoundation 0x000000010961bf34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000109531c15 ___forwarding___ + 1013
4 CoreFoundation 0x0000000109531798 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000106ddeb88 -[UIApplication sendAction:to:from:forEvent:] + 83
6 UIKit 0x0000000106f642b2 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x0000000106f645cb -[UIControl _sendActionsForEvents:withEvent:] + 444
8 UIKit 0x0000000106f634c7 -[UIControl touchesEnded:withEvent:] + 668
9 UIKit 0x0000000106e4c0d5 -[UIWindow _sendTouchesForEvent:] + 2747
10 UIKit 0x0000000106e4d7c3 -[UIWindow sendEvent:] + 4011
11 UIKit 0x0000000106dfaa33 -[UIApplication sendEvent:] + 371
12 UIKit 0x00000001075ecb6d __dispatchPreprocessedEventFromEventQueue + 3248
13 UIKit 0x00000001075e5817 __handleEventQueue + 4879
14 CoreFoundation 0x0000000109551311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15 CoreFoundation 0x000000010953659c __CFRunLoopDoSources0 + 556
16 CoreFoundation 0x0000000109535a86 __CFRunLoopRun + 918
17 CoreFoundation 0x0000000109535494 CFRunLoopRunSpecific + 420
18 GraphicsServices 0x000000010c383a6f GSEventRunModal + 161
19 UIKit 0x0000000106ddcf34 UIApplicationMain + 159
20 Learn ABCs 0x0000000105c1fa7f main + 111
21 libdyld.dylib 0x000000010a4d668d start + 1)
libc++abi.dylib: terminating with uncaught exception of type NSException
I feel like I have the : correct, so I'm not sure what else to look for. I'm using swift 3.0.
回答1:
Your Selector(("buttonAction:"))
might be the cause of your problem.
Try #selector(buttonAction(sender:))
instead.
回答2:
use this
func setUpButtons(){
for i in 1...2 {
let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 100, width: 75, height: 100))
btn.center = CGPoint(x: 20 + 100.0 * CGFloat(i), y: 200)
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("click", forState: UIControlState.Normal)
btn.addTarget(self, action:#selector(buttonAction), forControlEvents: .TouchUpInside)
self.view.addSubview(btn)
}
}
回答3:
for Swift 4 -
override func awakeFromNib() {
button.addTarget(self, action: #selector(buttonClicked), for: UIControlEvents.touchUpInside)
}
@objc func buttonClicked(sender:UIButton) {
...
}
回答4:
For iOS Facebook SDK Button,
Assign Button class from UIButton to FBSDKLoginButton, then run the application
It works while setting delegate, button permissions.
来源:https://stackoverflow.com/questions/39556442/uibutton-causing-unrecognized-selector-sent-to-instance