2016-09-18 2 views
-1

for 루프를 사용하여 여러 개의 버튼을 만들려고하는데 (발신인 :) 기능을 사용하는 데 문제가 있습니다. 내가 가지고있는 것처럼인식 할 수없는 선택기가 인스턴스로 전송되는 UIButton

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 느낌 :

내가 다음과 같은 오류를 표시하는 버튼을 누르면 다음 코드

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

이 올바른을, 그래서 나는 확실하지 않다 무엇을 찾을 다른 사람. 신속한 3.0을 사용하고 있습니다.

+1

가능한 중복 http://stackoverflow.com/questions/24007650/selector-in-swift – Moritz

답변

3

Selector(("buttonAction:"))이 문제의 원인 일 수 있습니다.

대신 #selector(buttonAction(sender:))을 시도하십시오.

1

사용이

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

스위프트 4 -의

override func awakeFromNib() { 
     button.addTarget(self, action: #selector(buttonClicked), for: UIControlEvents.touchUpInside) 
    } 

    @objc func buttonClicked(sender:UIButton) { 
     ... 
    } 
관련 문제