swift - 由于未捕获的异常“NSInvalidArgumentException”原因而终止应用程序:“-[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)
}
}
}
调用这个pickTheImage()
来自DemoApplicationController
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)
}
}
我越来越喜欢的错误
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
语法,我建议指定类而不是实例。这阐明了选择器本身引用了函数的名称,而不是函数本身。调用该函数的实例是目标。因此:
setProfilePhoto.addTarget(demoController,
action: #selector(DemoApplicationController.pickTheImage),
for: UIControl.Event.touchUpInside)
推荐阅读
- vba - excel/vba 中的模糊匹配
- android - 在 ViewPager 和 Recyclerview 中自动播放视频,如 instagram
- node.js - 如何重用 graphQl 解析器
- javascript - 样式化组件 v5.3 createGlobalStyles 不起作用
- multithreading - 为什么这会抛出 java.lang.IllegalMonitorStateException
- tensorflow - 连接两个相同维度的输出层
- c# - 通过 context.MergedJobDataMap 使用 JobChainingJobListener 在作业之间传递数据
- python - 使用python从pdf中提取特定数据到excel
- latex - (页)MS Word 文档 - 在 LaTeX 而不是 MathML 中导出方程?
- node.js - 在移动浏览器上对 toastify return 的错误做出反应