Pergunta

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'
Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top