tags:

views:

45

answers:

2

If I have a UIScrollView set up in the view via the Interface Builder, how do I get a reference to it in the ViewController implementation? I want to programmatically add labels to the scroll view.

For example, in C# if you have a textbox declared in the UI/form, you can access it by simply using the ID declared for that textbox. It doesn't seem this simple in objective c.

Thanks Kevin

+1  A: 

Assuming I understand you rightly and you are instantiating a view controller from a .xib containing subviews including the UIScrollView you want, there are two ways - first, you can find it in the subviews array that is owned by the view controller. Second, you can add an IBOutlet reference to it in your header file, then in interface builder make the connection using the connections inspector. Then you can refer to the object in your code, change frame, add labels etc.

Adam Eberbach
Yes, it sounds like you understand correctly.So there's a method of the ViewController that returns an array of all controls in the view?
Kevin
Every UIViewController has a view property which is a UIView. Every UIView has a property subviews which is an array of UIViews. However making the link in Interface Builder is going to be easier than going through the array and identifying the object you want especially as the number of views and subviews increases. It's just more convenient to have a persistent reference to something you want to access.
Adam Eberbach
Take a look at http://itunes.stanford.edu/ - follow the link to the Stanford iTunes U section then find the iPhone classes - alternatively just search for "CS193P" in the iTunes store search bar. Take a look at lectures 1 and 2, it is much easier to follow how to make the Interface Builder changes when you can watch someone else do it.
Adam Eberbach
Thank you. I wasn't aware of the iTunes courses. I will watch and see what I can learn.
Kevin
+1  A: 

You need to wire your ViewController up to your Nib files. This is pretty straightforward. This is your basic workflow for using Interface Builder on the iPhone/iPad:

  • Set the Class of the 'File's Owner' to the class of your view controller. You can do this by selecting the 'File's Owner' object in your nib window, pressing Command-4, and setting the class via the drop-down.
  • Create properties in your View Controller with the following format:

    @property (nonatomic, retain) IBOutlet UIScrollView *scrollView;

The IBOutlet keyword is a macro that evaluates to nothing. So it doesn't actually do anything to your code, it just exists to let Interface Builder know that the 'scrollView' property can be bound to.

  • Control-drag from the object you'd like to bind to your ViewController. In the popup you can select the property you'd like to bind to the scroll view object.

This sort of stuff is pretty basic Xcode stuff. If you read any tutorial out there it'll cover this. Good luck, and enjoy!

edit: I should add that if you used the default "New UIViewController Subclass" from the New File dialog, it will have done step one for you. You'll have a nib file and a View Controller that already know about each other.


  1. Yes, the code you wrote is all you need in your header. Just make sure you connect the Scroll View object to the property in Interface Builder.
  2. Yup! 'viewDidLoad' is added after all the connections specified in the Nib file have been made, so you can be confident that scrollView point to the correct object (assuming everything in the Nib is wired correctly, which is an easy mistake to make)
kubi
I did indeed use the New UIViewController Subclass from the File dialog. I also already added the property in the header, and used the Synthesize keyword in the implementation.Here is what my header for the ViewController looks like. Is it then as simple as calling/manipulating the control by name (scroller, in this case)?#import <UIKit/UIKit.h>@interface MyViewController : UIViewController { UIScrollView *scroller;}@property (nonatomic, retain) IBOutlet UIScrollView *scroller;@end
Kevin
Assuming I have it wired up correctly, how could I add a view to the Scroll View in the ViewDidLoad event? Is it as simple as "[scrollView addSubview:label];"
Kevin
I edited my answer for your questions.
kubi
BTW, I really recommend reading through a few Interface Builder tutorials. This stuff is easy once you get the hang of it, but it's also something that everyone royally screws up when they're learning.
kubi