繁体   English   中英

从不兼容类型分配给Nullable

[英]Assigning to ID Nullable from Incompatible Types

我在XCode中显示了我似乎无法摆脱的警告。

从不兼容类型'MyViewController * const _strong'分配给'id <UINavigationControllerDelegate,UIImagePickerControllerDelegate> _Nullable'

在这条线上:

picker.delegate = self;

这行代码使应用程序按预期工作。 因此删除它是行不通的。 但是我不知道如何摆脱错误。 有什么帮助吗?

指派了委托的其他方法不会引发此警告。

视图控制器是嵌入在NavigationController中的TabBarViewController的一部分。

我的课继承了UIImagePickerControllerDelegate

@interface MyViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UIActionSheetDelegate, UICollectionViewDelegateFlowLayout> {

}
///...
@end

以及完整的方法。

- (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];
}

在此处输入图片说明

除了UIImagePickerControllerDelegate之外,还需要UINavigationControllerDelegate

@interface MyViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, UICollectionViewDelegateFlowLayout> {

}

UINavigationControllerDelegateUIImagePickerControllerDelegate一起添加对我隐藏了该警告。

在UIKit中,图像选择器的委托指定为:

@property(nullable,nonatomic,weak) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;

从文档中:

图像选择器的委托对象。

宣言

迅速

weak var delegate: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>?

目标C

@property(nonatomic, weak) id<UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate

讨论区

当用户选择图像或电影或退出选择器界面时,委托会收到通知。 委托还决定何时关闭选择器界面,因此您必须提供一个委托才能使用选择器。 如果此属性为nil,则尝试显示该选择器会立即将其关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM