[英]Assigning to ID Nullable from Incompatible Types
I have this warning showing up in XCode that I can't seem to get rid of. 我在XCode中显示了我似乎无法摆脱的警告。
Assigning to 'id < UINavigationControllerDelegate, UIImagePickerControllerDelegate > _Nullable ' from incompatibale type 'MyViewController * const _strong'
从不兼容类型'MyViewController * const _strong'分配给'id <UINavigationControllerDelegate,UIImagePickerControllerDelegate> _Nullable'
on this line: 在这条线上:
picker.delegate = self;
This line of code causes the application to work as expected. 这行代码使应用程序按预期工作。 So removing it, it doesn't work.
因此删除它是行不通的。 But I don't know how to get rid of the error.
但是我不知道如何摆脱错误。 Any help?
有什么帮助吗?
Other methods that have delegate assigned are not throwing this warning. 指派了委托的其他方法不会引发此警告。
The view controller is a part of a TabBarViewController that is embedded in a NavigationController. 视图控制器是嵌入在NavigationController中的TabBarViewController的一部分。
My class inherits the UIImagePickerControllerDelegate
我的课继承了
UIImagePickerControllerDelegate
@interface MyViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UIActionSheetDelegate, UICollectionViewDelegateFlowLayout> {
}
///...
@end
And the complete method. 以及完整的方法。
- (void) showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.modalPresentationStyle = UIModalPresentationCurrentContext;
picker.sourceType = sourceType;
picker.delegate = self; ///HERE IS THE ISSUE
picker.modalPresentationStyle = UIModalPresentationFullScreen;
picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
if (sourceType == UIImagePickerControllerSourceTypeCamera) {
picker.showsCameraControls = YES;
}
[self presentViewController:picker animated:YES completion:nil];
}
You need both the UINavigationControllerDelegate
in addition to UIImagePickerControllerDelegate
. 除了
UIImagePickerControllerDelegate
之外,还需要UINavigationControllerDelegate
。
@interface MyViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, UICollectionViewDelegateFlowLayout> {
}
Adding UINavigationControllerDelegate
along with the UIImagePickerControllerDelegate
hides that warning for me. 将
UINavigationControllerDelegate
与UIImagePickerControllerDelegate
一起添加对我隐藏了该警告。
In the UIKit the delegate for image picker is specified as: 在UIKit中,图像选择器的委托指定为:
@property(nullable,nonatomic,weak) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;
From the Documentation: 从文档中:
The image picker's delegate object. 图像选择器的委托对象。
Declaration 宣言
SWIFT 迅速
weak var delegate: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>?
OBJECTIVE-C 目标C
@property(nonatomic, weak) id<UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate
Discussion 讨论区
The delegate receives notifications when the user picks an image or movie, or exits the picker interface. 当用户选择图像或电影或退出选择器界面时,委托会收到通知。 The delegate also decides when to dismiss the picker interface, so you must provide a delegate to use a picker.
委托还决定何时关闭选择器界面,因此您必须提供一个委托才能使用选择器。 If this property is nil, the picker is dismissed immediately if you try to show it.
如果此属性为nil,则尝试显示该选择器会立即将其关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.