2016-08-18 10 views
0

XCodeでこの警告が表示されない場合があります。互換性のない型からID Nullableに割り当てる

picker.delegate = self;

コード原因の行を:incompatibaleタイプから 'ID < UINavigationControllerDelegate、UIImagePickerControllerDelegate> _Nullable' に 'MyViewController * CONST _strong'

このラインに割り当て

アプリケーションが期待どおりに動作するようにします。 それを削除するので、動作しません。しかし、私はエラーを取り除く方法を知らない。どんな助け?

代理人が割り当てられている他のメソッドは、この警告をスローしません。

View Controllerは、NavigationControllerに埋め込まれたTabBarViewControllerの一部です。

私のクラスはUIImagePickerControllerDelegate

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

} 
///... 
@end 

、完全なメソッドを継承します。 UIImagePickerControllerDelegateが私のために警告することを隠蔽するとともにUINavigationControllerDelegateを追加

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

enter image description here

答えて

0

UIImagePickerControllerDelegateに加えて、UINavigationControllerDelegateの両方が必要です。

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

} 
+0

問題が修正されました。あなたは、それが要件であると書かれているいくつかの文書への素早いリンクを持っていますか?なぜ他の代表者がその警告を発したのですか? –

+0

ドキュメントは、 'UIImagePickerController'の' delegate'プロパティのために見つかりました。それは '@property(nullable、nonatomic、weak)id delegate;'として宣言されています。つまり、プロパティに割り当てられているものは、両方のプロトコルに準拠している必要があります。 – rmaddy

+0

ありがとうございました。これは私がちょうど記憶にコミットしなければならないものです。 –

0

画像ピッカーのデリゲートオブジェクト:ドキュメントから

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

:イメージピッカーのためのデリゲートは次のように指定されているのUIKitで

宣言

SWIFT

weak var delegate: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>? 

Objective-Cの

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

議論

デリゲートは、ユーザーが画像やムービーを選択したとき、またはピッカーインターフェイスを終了したときに通知を受け取ります。デリゲートはまた、ピッカーインターフェイスを閉じるタイミングを決定するため、ピッカーを使用するためにデリゲートを提供する必要があります。このプロパティがnilの場合、表示しようとするとすぐにピッカーは終了します。

+0

「UINavigationControllerDelegate」は警告を処理します。 'UINavigationBarDelegate'は行いません。 –

+0

はい、あなたは正しいです、私はそれをタイプミス。それはその警告を隠すべきです。 @ Firemarble –

関連する問題