xonegirlz
xonegirlz

Reputation: 8967

copyWithZone issue

I am trying to make a copy of my UIViewController subclass by doing:

BookViewController *bookVC = [catalogFlatViewController copy];

and I have the following error:

'-[BookViewController copyWithZone:]: unrecognized selector sent to instance 0x8e5f00'

Upvotes: 2

Views: 2021

Answers (2)

Parth Bhatt
Parth Bhatt

Reputation: 19469

Refer to @Caleb answer in the below post:

iOS copyWithZone unrecognized selector only when using device

Caleb's answer is as under

UIViewController doesn't implement the NSCopying protocol. Unless you've implemented NSCopying in your own UIViewController subclass, your view controller won't respond to -copyWithZone:.

For more information: you may want to refer to UIViewController class reference.

But I would suggest you not to implement NSCopying Protocol Reference, as @RobNapier has given one of the very nice and informative article at Implementing NSCopying (or NSCopyObject() considered harmful)

Hope this helps.

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299355

UIViewController does not conform to NSCopying. If you want to make a copy, you have to use NSCoding:

NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:controller];
UIViewController *newController = [NSKeyedUnarchiver unarchiveObjectWithData:archive];

If you've added new ivars to your view controller, you'll have to override the NSCoding methods (encodeWithCoder: and initWithCoder:) to serialize and deserialize these correctly. See Archives and Serializations Programming Guide.

BTW, this is basically how a nib file works.

Upvotes: 3

Related Questions