簡體   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