tags:

views:

202

answers:

2

Hi, I have a relatively simple app. I have a fullscreen UIView subclass (musicGridView) that the user interacts with, and a UIScrollView subclass (scrollView) that loads this class in, as well as a ViewController and mainWindow.xib.

Right now, I have a viewController instance loaded in the nib, which is of type myViewController. The nib also has instance of myUIView, which the myViewController is pointed to.

In AppDelegate, I do this:

[window addSubview:scrollView];
[scrollView addSubview: viewController.view];
scrollView.musicGridView = (MusicGridView*) viewController.view;

which I think is wrong. Ideally, the appDelegate doesn't have an instance of scrollView.

In scrollView's AwakeFromNib class, I do the initialization on the scrollView.

The way I think it should go, is load the [window addSubview:viewController.view] in appDelegate, and then point the viewController to an instance of scrollView instead of musicGridView. Then, in scrollView awakeFromNib add a subview of musicGridView.

Eventually, I want to create another view owned by scrollView that is actually a side TabBar (which isn't possible with the given API's) that the user can scroll to the left to reach.

So I guess amongst other MVC stuff, the question is, should the viewController point to the scrollView, which contains all other content UIView subclasses, including musicGridView?

A: 

It sounds like you are not using Interface Builder to design your UI. Since this is a new project, I would suggest doing that. You should not have to write any code like this at all. You will however need to describe your outlets and actions.

The two most important things you need to know when dealing with IB are the IBOutlet and IBAction keywords.

Sample class header:

@interface MyClass : NSObject {
  IBOutlet UIScrollView* myScrollView;
}

- (IBAction) doWork;

@end

Now you can wire these two things up using Interface Builder by dragging sources to destinations with your mouse. YouTube has lots of tutorial videos on IB if you need a better description of how to do this.

slf
A: 

I'm someone who doesn't use Interface Builder for UI design; in my experience, you need to go either all-IB, or all-programmatic. To solve your particular problem, I think you need your musicGridView to be an instance or extension of UIScrollView.

This can be done in your custom viewController's loadView method - simply initialize it to a UIScrollView (and add things to it), instead of a plain old UIView.

However, as described above, this approach isn't compatible with an IB-centric approach, as is confirmed in the UIViewController Class Reference

All subclasses of UIViewController should implement the loadView method to set the view property unless you load your view from a nib file.

Glenn Barnett