I am using xib to create view for my project. The condition is:
I have multiple UIView IBoutlet's Object.
IBOutlet UIView *viewOpenDoor;
IBOutlet UIView *viewOpenDoor_Second;
viewOpenDoor is only connected to one of the view in xib.
Now i am using this code to reuse the same view multiple times in viewdidload method-
[viewOpenDoor setFrame:CGRectMake(30, 80, viewOpenDoor.frame.size.width, viewOpenDoor.frame.size.height)];
[self.view addSubview:viewOpenDoor];
viewOpenDoor.layer.borderColor = [UIColor blackColor].CGColor;
viewOpenDoor.layer.borderWidth = 0.9f;
viewOpenDoor.layer.cornerRadius = 6.0f;
[viewOpenDoor setHidden:YES];
viewOpenDoor_Second = [[UIView alloc] init];
viewOpenDoor_Second = [viewOpenDoor copy];
[viewOpenDoor_Second setFrame:CGRectMake(184, 80, viewOpenDoor.frame.size.width, viewOpenDoor.frame.size.height)];
[self.view addSubview:viewOpenDoor_Second];
it's giving exception-
-[UIView copyWithZone:]: unrecognized selector sent to instance 0x95ba140
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView copyWithZone:]: unrecognized selector sent to instance 0x95ba140'
So, my question is how can i reuse this one IBOutlet object created in xib, multiple times with different instances?
You load the xib into a UINib and instantiate all the copies you want from that UINib.
Then access the outlet of the copy of the whole xib. You cant copy a view, you can only instatiate multiple "copies" from the same UINib.
You can store the UINib in an instance variable if you plan to create more later.
Try this:- Reference all the instances(that you want to connect) of UIView to XIB file as shown in below image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With