UIImageView initWithCoder - unrecognized selector

2020-04-20 23:50发布

问题:

I'm sorry if it has already been asked, but here comes the problem:

I've recently updated my SDK to iOS 5 beta. My app builds and runs perfectly fine on the new simulator, but when I try to change the Deployment Target to the older iOS (4.3), the app crashes instantly when it tries to use UIActivityIndicatorView control (both simulator and device). I'm using the UIActivityIndicatorView in a really simple loading view consisting only of a View and UIActivityIndicatorView.

When I remove UIActivityIndicatorView control from that view, the view works fine, BUT the app crashes while loading other views (with activity indicators and images). So, the long story short: app works fine in the iOS 5 environment, but images and activity indicators crash it while running it on older iOS versions. Any ideas?

Thanks in advance!

EDIT:

I'm adding some code just to show you guys how I'm using the view. It's really not much and the LoadingViewController itself has absolutely no custom code (all controls are in the nib).

if(!self.loadingScreen){
    self.loadingScreen = [[LoadingViewController alloc] initWithNibName:@"LoadingViewController" bundle:nil];
}
[self.view addSubview:loadingScreen.view];

The exception message doesn't actually say much more, but here you go:

0   CoreFoundation                      0x017bd5a9 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x01911313 objc_exception_throw + 44
2   CoreFoundation                      0x017bf0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3   CoreFoundation                      0x0172e966 ___forwarding___ + 966
4   CoreFoundation                      0x0172e522 _CF_forwarding_prep_0 + 50
5   UIKit                               0x00f8e9fd UINibDecoderDecodeObjectForValue + 2592
6   UIKit                               0x00f8f6ac -[UINibDecoder decodeObjectForKey:] + 398
7   UIKit                               0x00d75db0 -[UIImageView initWithCoder:] + 97

回答1:

I had the same problem and just found a solution. My UIActivityIndicator was added into a xib file. I have removed it from the xib and have created it by code (activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:...).

All is ok now, it works !

I hope this will helps you.



回答2:

I can't open URL https://devforums.apple.com/message/507796#507796.... but I think you can make a category for UIImage class, like this:

@implementation UIImage (Coder)

- (id)initWithCoder:(NSCoder *)aCoder
{
     return nil;
}

@end


回答3:

Following smith324's advice I started to browse Apple dev forums to find out if I'm doing something wrong or if there's a bug in the SDK. It turns out, that there's a nasty bug in the latest release crashing UIImages, UIActivityIndicators etc. just like in my case. Good news is, there actually is a workaround and you can find it here: https://devforums.apple.com/message/507796#507796

Thanks everyone for your help!

EDIT:

If you can't access the link, this is the suggested answer:

@implementation UIImage (initWithCoder)

- (id)initWithCoder:(NSCoder *)aDecoder
{
     return nil;
}

@end