0
votes

I create a custom UIView Object using the interface builder. This view contains a UILabel and an UIImageView.

I placed this object using interface builder in a ViewController and set the class of the UIView to UIViewLabelImage. I referenced the UILabel and an UIImageView in that class .h file . I also create a CustomOutlet to manipulate this new UIViewLabelImage Object.

enter image description here I managed to retrieve the object UIViewLabelImage using :

  UIViewLabelImage *view = (UIViewLabelImage *)[self.view viewWithTag:101]; 
  //or using self.CustomOutlet.

I’am able to change the view, the text and image everything works great.

  self.CustomOutlet.img.backgroundColor = [UIColor redColor];

If i want to add another UIViewLabelImage programmatically it works too. I Retrieve that newly created object using the new tag for example.

But i have a problem when i try to copy the UIViewLabelImage. The copy work and the object presented on the screen is the same as the first one but i am not able to change the text or image of that label. This is because the copyOFLabel.text and copyOFLabel.img are nil.

  UIViewLabelImage *copyOFLabel = [NSKeyedUnarchiver unarchiveObjectWithData:
  [NSKeyedArchiver archivedDataWithRootObject:self.CustomOutlet]];
  copyOFLabel.tag = 11;
  //copyOfLabel.text == nil 
  copyOFLabel.text.text = @"Copy Label";
  copyOFLabel.backgroundColor = [UIColor greenColor];
  copyOFLabel.frame = CGRectOffset(copyOFLabel.frame, 100, 200);
  //view Added same as self.CustomOutlet expect for the background color set to green
  [self.view addSubview:copyOFLabel];

Is there a way to copy that UIViewLabelImage and keep the image and text referenced in the text and img properties of the UIViewLabelImage variable ?

1
Do you need to add the second one in code, or can you set it up in the storyboard? Are you adding this second one to the same view controller as the first one? – rdelmar
yes i am adding this second one to the same viewController. I want to add the second one by code because i don't know in advance how many of them i need to have. But i know that i will always have at least one. That why i set the first one in IB and set the color and other properties directly in the IB and want to reuse a copy of it and only change the content img and text content. – Aaleks
If you don't know how many you're going to have, it might be better to use a table view instead. Your custom view would then be a custom table view cell. Otherwise, if you have very many, you'll have to put them in a scroll view and handle all the placement your self. A table view could give you the same look with much less effort. – rdelmar

1 Answers

0
votes

All you're doing is creating a new class, setting some variables that you can change using IB. So after your viewWillAppear in your class you have a reference to that specific object and it's values that were in IB (image, text, etc). Now that you have a reference to your existing object you just need to set those same values to the newly created one. If you are looking to clone the exact view then that's a different answer.

newCustomObject.img = self.CustomOutlet.img
// and so on

If you're looking to create a "clone" of your existing view you can do that with NSKeyedArchiver in some cases.

id copyOfView = 
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:originalView]];

My advice is to simply use IB as a template for your custom class then assign the images and text in code after you create a new instance.