2016-08-18 32 views
0

我在XCode中顯示了我似乎無法擺脫的此警告。從不兼容類型中分配ID可空的

分配到 'ID < UINavigationControllerDelegate,UIImagePickerControllerDelegate> _Nullable' 從incompatibale類型 'MyViewController * const的_strong'

在這條線:

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

enter image description here

回答

0

除了UIImagePickerControllerDelegate之外,您還需要UINavigationControllerDelegate

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

} 
+0

解決了問題。你碰巧有一個快速鏈接到一些文件,說明這是一個要求?其他代表爲什麼會發出這樣的警告? –

+0

該文檔是爲'UIImagePickerController'的'delegate'屬性找到的。它被聲明爲@屬性(可空,非原子,弱)id 委託;'。這意味着無論分配給財產必須符合這兩個協議。 – rmaddy

+0

非常感謝。這是我必須承諾記憶的一個。 –

0

添加與UIImagePickerControllerDelegate一起UINavigationControllerDelegate隱藏了警告我。

在的UIKit用於圖像拾取委託被指定爲:

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

從文檔:

所述的圖像拾取器的委託對象。

宣言

SWIFT

weak var delegate: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>? 

目的-C

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

討論

委託人在用戶選擇圖像或電影或退出選取器界面時收到通知。代表還決定何時關閉選取器界面,因此您必須提供一個代表來使用選取器。如果此屬性爲零,則如果您嘗試顯示該拾取器,則立即將該拾取器解散。

+0

'UINavigationControllerDelegate'確實處理了警告。但是'UINavigationBarDelegate'沒有。 –

+0

是的,你是對的,我想念它。它應該隱藏該警告。請清理您的項目並建立安捷利來查看更改。@ Firemarble –

相關問題