Hi - I'm fairly new to iOS programming, I've just managed to make a view completely programatically (no interface builder)...but now I'm wondering how I access the different elements such as a UILabel or UISwitch etc. In IB I'd just connect them up to their respective outlets / ibactions, how do I do it when I've created it the view from code?
If you created them in code, you can keep a reference to the objects your create. For instance by making a property (nonatomic, retain usually) and setting it when you create the object.
Well, have you created the views contents programatically too? If so, all you need to do is keeping a reference to each element. Once you create a new UI element, just assign it to some prepared property, is the programmatic substitute for connecting elements in IB to instance variables / properties.
Is your view a custom class, or have you just created a UIView and added subviews to it?
If you wrote a custom view class, you can make properties to refer to all these things. Then you can do yourView.aSwitch
(or whatever your property is called) to refer to them from the controller.
If you just created a UIView and added subviews, there's two things you can do. The best option is, at the place where you're creating the switch (or whatever), keep a reference to it around, perhaps in an instance variable of your controller class. Thus you might do
UIView *aView = [[UIView alloc] initWithRect:aRect];
UISwitch *switch = [[UISwitch alloc] init];
[aView addSubview:switch];
// your variable 'switch' is a reference to the switch. Assign it to an instance variable of your controller class here.
The other way you can do it (and this one isn't really recommended) is to iterate through the subviews of your view until you find the one you want. The subviews
property of UIView gives you an array of them.