由于未捕获的异常 'NSInvalidArgumentException' 原因终止应用程序:'-[Demo_App.EnrollCell pickTheImage]

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

我试图从 DemoApplicationController 调用方法,以便从中调用 imagePickerController。但是得到像上面这样的错误

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

EnrollCell.Swift

调用 DemoApplicationControllerpickTheImage()
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)

}
}

错误我正在变得像

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

如果你写 setProfilePhoto.addTarget(self /* ... */)pickTheImage 选择器将被发送到 self,在你的情况下它是一个单元格,而不是视图控制器。

您需要将其更改为您要将操作发送到的实例,在您的情况下为演示控制器。 此外,对于 #selector 语法,我建议指定 class 而不是实例。这澄清了选择器本身引用函数的 name,而不是函数本身。函数被调用的实例是目标。 因此:

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