【问题】
iOS项目中,给一个controller设置一个delegate时,出现警告:
/Users/crifan/develop/ios_sdk/iphone/BirdWatching/BirdWatching/BirdWatching/AddSightingViewController.m:60:39: Assigning to ‘id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>’ from incompatible type ‘AddSightingViewController *const __strong’
【解决过程】
1.问题很明显是 strong不兼容,然后就去试试,把相应的AddSightingViewController改一下,从
@property (nonatomic) UIImagePickerController *imgPickerController;
改为:
@property (nonatomic, weak) UIImagePickerController *imgPickerController;
然后就消除了此警告了。
注:有时候Xcode不能及时消除警告和错误,需要手动去Product->Clean一下。
【总结】
好像Property变量,如果不指定引用类型,则默认是strong引用,此处改为weak,即可解除此警告。
而另外关于weak,strong等更详细的含义,有空再整理。
【后记】
实际上,刚才此问题根本没有解决。
现在继续折腾。
1.找到:
其解释和我此处不符合,因为我已经在头文件中声明了:
@interface AddSightingViewController : UITableViewController <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate, CLLocationManagerDelegate, MKMapViewDelegate, UIImagePickerControllerDelegate> { NSArray *birdNameList; CLLocationCoordinate2D location2D; }
但是还是有警告。
2.后来找到这里:
WARNING when adding UIImagePickerController
其解释很清楚:
主要是UIImagePickerController
有两个protocol:UINavigationControllerDelegate和UIImagePickerControllerDelegate,而此处当前对象self为AddSightingViewController,其只在.h中定义了,符合UIImagePickerControllerDelegate,而没有说也符合UINavigationControllerDelegate,因此才报错的。
其解释,也和我刚才猜想的一致。
只是我刚才在官网:
UIImagePickerControllerDelegate Protocol Reference
中没有看到这样:
@property (nonatomic, assign) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;
的定义罢了,所以以为自己的理解有误呢。
然后又发现,其实就是自己没注意看,因为上述的delegate的定义,本来就不在UIImagePickerControllerDelegate中,而在
UIImagePickerController Class Reference
中,对应定义为:
delegate
The image picker’s delegate object.
@property (nonatomic, assign) 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.For information about the methods you can implement for your delegate object, see UIImagePickerControllerDelegate Protocol Reference.
Availability
- Available in iOS 2.0 and later.
Related Sample Code
Declared In
UIImagePickerController.h
但是很无语的是,刚开始看官网文档的时候,其解释为:
Providing a Delegate Object
To use an image picker controller, you must provide a delegate that conforms to the
UIImagePickerControllerDelegate
protocol. Starting in iOS 4.1, you can use the delegate to save still-image metadata to the Camera Roll along with the image. See UIImagePickerControllerDelegate Protocol Reference.
很明显,其并没有提及UINavigationControllerDelegate,而只说要符合UIImagePickerControllerDelegate,导致写代码的之后,肯定也只会添加一个delegate:UIImagePickerControllerDelegate,而不会想到UINavigationControllerDelegate。
所以,问题很明确了,就是去添加对应的声明,符合UINavigationControllerDelegate即可。
即改为:
@interface AddSightingViewController : UITableViewController <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate, CLLocationManagerDelegate, MKMapViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> { NSArray *birdNameList; CLLocationCoordinate2D location2D; }
结果很悲催的,点击那个图片后,没有出现预料的Album,却在presentViewController的位置出错了:
所以,也要接着处理这个问题。
结果发现是上面的UIImagePickerController变量更改导致的,即再从:
@property (nonatomic, weak) UIImagePickerController *imgPickerController;
改回:
@property (nonatomic) UIImagePickerController *imgPickerController;
就可以正常切换界面,到Album了。
结果,接下来,也就可以正常执行了。
【总结】
设置delegate时候,出现警告:Assigning to ‘id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>’ from incompatible type ‘AddSightingViewController *const __strong’
解决方法:
去.h中添加:
@interface xxx : xxx <xxx, UIImagePickerControllerDelegate, UINavigationControllerDelegate> { ... }
以符合UIImagePickerControllerDelegate和UINavigationControllerDelegate,即可解决问题。
转载请注明:在路上 » 【已解决】iOS程序中,设置delegate时候,出现警告:Assigning to ‘id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>’ from incompatible type ‘AddSightingViewController *const __strong’