0

Basically, my problem is exactly what it says in the title. When I try to encode a subclass of UIViewController, calling [super encodeWithCoder] gives an NSInvalidArgumentException. Specifically, I get -[UIImage encodeWithCoder:]: unrecognized selector sent to instance XxXXXXXX.

The only image image in the view is on a UIButton, which is also supposed to conform to NSCoding, and the stack trace includes a call to [UIBUtton encodeWithCoder]. The button is created programmatically with [UIButton buttonWithType:UIButtonTypeCustom], and the image is set with setImage: forState:. I really have no idea what is going on here. Am I missing something obvious, or does UIButton just not really conform to NSCoding?

Mutix
  • 4,196
  • 1
  • 27
  • 39
Ethan Holshouser
  • 1,402
  • 14
  • 13

1 Answers1

1

I can't imagine why you would want to be archiving view objects within your app, but you probably need to add NSCoding support yourself by writing a category on UIImage.

For details see: iPhone - Why does the documentation say UIImageView is NSCoding compliant?

View objects support NSCoding because the view loading system uses it to load objects from Nib files. But the UINib class does some additional work that NSKeyedArchiver does not.

If you just want to store state between launches, it's better to store data and not view state. Especially if you ever plan to update your app and make any changes whatsoever to the view layout. (This is the motivation behind Model/View/Controller separation.)

Community
  • 1
  • 1
benzado
  • 82,288
  • 22
  • 110
  • 138
  • So basically, conformance to NSCoding doesn't actually mean what Apple says it means. Thanks for the link, I searched but I didn't come across that. – Ethan Holshouser Oct 03 '11 at 06:43
  • 1
    It means what it says, it just doesn't say what you think. For instance, a NSCoding class might support keyed archiving, or classic archiving, or both, or neither. There are no guarantees. – benzado Oct 03 '11 at 17:11