Search code examples
swiftuicollectionviewuicollectionviewcelluiimagepickercontroller

Terminating app due to uncaught exception 'NSInvalidArgumentException' reason: '-[Demo_App.EnrollCell pickTheImage]


I was trying to call a method from DemoApplicationController in order to call a imagePickerController from it. But getting a error like the above

DemoApplicationController.Swift

extension DemoApplicationController : UIImagePickerControllerDelegate,UINavigationControllerDelegate,PickTheImageDelegate{
    var enroll : EnrollCell?
  @IBAction func pickTheImage() {
        
        let vc = UIImagePickerController()
        vc.sourceType = .photoLibrary
        vc.delegate = self
        vc.allowsEditing = true
        present(vc, animated: true, completion: nil)
        
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage {
                enroll?.profileIG.image=image
            }
            dismiss(animated: true, completion: nil)
        }
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            dismiss(animated: true, completion: nil)
        }
    }
   
}

Calling this pickTheImage() of DemoApplicationController from EnrollCell.Swift

class EnrollCell: UsersCell,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    var demoController = DemoApplicationController()
   
    let profileIG : UIImageView = {
        let imageView = UIImageView()
//        imageView.image = UIImage(named: "1")
        imageView.backgroundColor = UIColor(red: 0.12, green: 0.56, blue: 1.00, alpha: 1.00)
        imageView.layer.cornerRadius=22
        imageView.layer.masksToBounds=true
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    }()
    
    let setProfilePhoto: UIButton = {
        let profile = UIButton()
        profile.setTitle("Select Profile Photo", for: .normal)
        profile.setTitleColor(UIColor(red: 0.12, green: 0.56, blue: 1.00, alpha: 1.00), for: .normal)
        profile.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 16)
        profile.translatesAutoresizingMaskIntoConstraints = false
        return profile
    }()

  

    lazy var collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = .white
        cv.delegate = self
        return cv
    }()

    
    override func setupViews() {
        addSubview(profileIG)
        addSubview(setProfilePhoto)
  
        setProfilePhoto.addTarget(self, action: #selector(demoController.pickTheImage), for: UIControl.Event.touchUpInside)

        addConstraintsWithFormat("H:|-150-[v0(100)]-150-|", views: profileIG)
        addConstraintsWithFormat("V:|-50-[v0(100)]-16-|", views: profileIG)
        print(frame.width)
        
        
        addConstraintsWithFormat("H:|-105-[v0(\(frame.width/2))]|", views: setProfilePhoto)
        addConstraintsWithFormat("V:|-150-[v0(35)]|", views: setProfilePhoto)

}
}

error I was getting like

2021-03-04 12:09:48.154393+0530 Demo_App[26922:1081464] -[Demo_App.EnrollCell pickTheImage]: unrecognized selector sent to instance 0x7f950cd4f730
2021-03-04 12:09:48.163081+0530 Demo_App[26922:1081464] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Demo_App.EnrollCell pickTheImage]: unrecognized selector sent to instance 0x7f950cd4f730'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff20421af6 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff20177e78 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff204306f7 +[NSObject(NSObject) instanceMethodSignatureForSelector:] + 0
    3   UIKitCore                           0x00007fff246cba43 -[UIResponder doesNotRecognizeSelector:] + 292
    4   CoreFoundation                      0x00007fff20426036 ___forwarding___ + 1489
    5   CoreFoundation                      0x00007fff20428068 _CF_forwarding_prep_0 + 120
    6   UIKitCore                           0x00007fff2469d19e -[UIApplication sendAction:to:from:forEvent:] + 83
    7   UIKitCore                           0x00007fff23fc6684 -[UIControl sendAction:to:forEvent:] + 223
    8   UIKitCore                           0x00007fff23fc69a7 -[UIControl _sendActionsForEvents:withEvent:] + 332
    9   UIKitCore                           0x00007fff23fc5290 -[UIControl touchesEnded:withEvent:] + 500
    10  UIKitCore                           0x00007fff246d984e -[UIWindow _sendTouchesForEvent:] + 1287
    11  UIKitCore                           0x00007fff246db6c7 -[UIWindow sendEvent:] + 4774
    12  UIKitCore                           0x00007fff246b5466 -[UIApplication sendEvent:] + 633
    13  UIKitCore                           0x00007fff24745f04 __processEventQueue + 13895
    14  UIKitCore                           0x00007fff2473c877 __eventFetcherSourceCallback + 104
    15  CoreFoundation                      0x00007fff2039038a __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    16  CoreFoundation                      0x00007fff20390282 __CFRunLoopDoSource0 + 180
    17  CoreFoundation                      0x00007fff2038f764 __CFRunLoopDoSources0 + 248
    18  CoreFoundation                      0x00007fff20389f2f __CFRunLoopRun + 878
    19  CoreFoundation                      0x00007fff203896d6 CFRunLoopRunSpecific + 567
    20  GraphicsServices                    0x00007fff2c257db3 GSEventRunModal + 139
    21  UIKitCore                           0x00007fff24696cf7 -[UIApplication _run] + 912
    22  UIKitCore                           0x00007fff2469bba8 UIApplicationMain + 101
    23  Demo_App                            0x000000010c71e80b main + 75
    24  libdyld.dylib                       0x00007fff2025a3e9 start + 1
    25  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Demo_App.EnrollCell pickTheImage]: unrecognized selector sent to instance 0x7f950cd4f730'
terminating with uncaught exception of type NSException

Solution

  • If you write setProfilePhoto.addTarget(self /* ... */), the pickTheImage selector will be send to self, which in your case is a cell, not the view controller.

    You need to change this to the instance you want to send the action to, in your case the demo controller. Also, for the #selector syntax, I would suggest to specify the class instead of an instance. This clarifies that a selector itself references the name of a function, not the function itself. The instance that the function is called upon is the target. Hence:

    setProfilePhoto.addTarget(demoController, 
                      action: #selector(DemoApplicationController.pickTheImage),
                         for: UIControl.Event.touchUpInside)