views:

47

answers:

1

Okay, I have a ViewController that has 12 UIButtons as instance variables, and they are created in interface builder. I want to access another variable in the viewController from the UIButton's init method. Is that possible?

By the way, I'm writing in Objective-C in the iPhone SDK.

Cheers.

A: 

Unless you explicitly add an ivar and property to your button (by subclassing UIButton) and assign the view controller to it, it (and any other view) has no access to its (or any of its superviews') view controller.

If you feel you require such a thing, it is often a sign of a badly thought-out design because if you follow the MVC pattern views should generally have no knowledge of the model or controller layer. What are trying to accomplish?

Ole Begemann
Like I said, the viewController has 12 UIButtons as variables named button1, button2, etc.. So, in the the init method of my UIButton subclass, I need to set a variable based on which button it is. Is there an easy way to do that?
Dyldo42
You could use the buttons' `tag` property (set it directly from IB) to give them an identifier. Or declare a property in the button class and set it from your view controller code (e.g. in `viewDidLoad`). As I said, you have no access to the view controller from the button.
Ole Begemann
But the thing is, I need to set that up in the UIButton subclass' init method, which (I'm pretty sure) is called before the views' viewDidLoad method. If worse comes to worse, I could move some of that init code to viewDidLoad.
Dyldo42
I went through and rearranged my code. I don't need this variable any more.
Dyldo42