0

I need to create an exact copy of several UIComponents in my app. An example of this is a UILabel. I have created two UILabels in interface builder, which I have then linked up.

The next thing I did was as follows:

self.myLabel_a.text = @"Blah blah blah";
// ... and some other settings (eg. font size, colour, etc.)
self.myLabel_b = self.myLabel_a;

However, when I run my app I do not see the desired effect. Please can someone advice on why this is happening. I'm very sorry if this is a novice question, however I do not understand the issue here and how to fix it.

  • What you are experiencing is the basics of how pointers work. `myLabel_a` is pointing at a `UILabel`. You then set `myLabel_b` to `myLabel_a` which will make it also point to the same `UILabel`. You need two separate `UILabel`s and if you want them to always display the same thing you need to write code to sync them. – Joe Mar 21 '12 at 19:08

1 Answers1

2

Your code now just assignes references, it's not a 'copying'. In order to support real copy you need to implement NSCopying protocol. UILabel does not conform to NSCopying, and you cannot just 'make a copy'.

See these questions below:

Community
  • 1
  • 1
beryllium
  • 29,669
  • 15
  • 106
  • 125