0

Currently I have a view UIView *myView;. I would like to add data to the view by saying something like myView.stringdata = @"whatever";. How can I do this? Sorry for the simple question I'm new to Xcode.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
JohnAnge Kernodle
  • 239
  • 1
  • 3
  • 20

2 Answers2

1

You can't add random data to views. You can subclass UIView if you want and add a property to the class and store your string there. Or you could make a property in the ViewController that is driving your UIVIew and assign the string to it.

jervine10
  • 3,039
  • 22
  • 35
  • this isnt necesarily true because he can make a category that holds a property and the works exactly as he was hoping – ASKASK Feb 20 '14 at 04:40
  • @ASKASK Incorrect. Categories **cannot** add instance variables or properties. This is what Apple says: `Categories can be used to declare either instance methods or class methods but are not usually suitable for declaring additional properties. It’s valid syntax to include a property declaration in a category interface, but it’s not possible to declare an additional instance variable in a category. This means the compiler won’t synthesize any instance variable, nor will it synthesize any property accessor methods.` – Amar Feb 20 '14 at 07:02
  • @ASKASK Workaround is using associative references. Refer this: http://stackoverflow.com/a/8733320/1407017 – Amar Feb 20 '14 at 07:09
0

You cannot add data to an instance of a class if the class does not have an ivar + accessor or a property for it.

You need to use one of the following approaches:

Subclass Category Delegate Associative Objects Composition Collections

All of these involve modifying the class as a whole in your app (or any app if it were a framework class in a framework visible at the user or system level) or they involve creating new classes or association by being in collection member.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55