简体   繁体   中英

Assigning to ID Nullable from Incompatible Types

I have this warning showing up in XCode that I can't seem to get rid of.

Assigning to 'id < UINavigationControllerDelegate, UIImagePickerControllerDelegate > _Nullable ' from incompatibale type 'MyViewController * const _strong'

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.

My class inherits the 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 .

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

}

Adding UINavigationControllerDelegate along with the UIImagePickerControllerDelegate hides that warning for me.

In the UIKit the delegate for image picker is specified as:

@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

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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