tags:

views:

2282

answers:

4

I am trying to work my way through basic iPhone programming and I have a good basic understanding of how Interface Builder works, so I decided to try my hand at doing the views programmatically. I have gone through the ViewController Apple guide and searched everywhere and I cannot seem to find a solution to my problem. This leads me to believe it is a very simple solution, but I am just really banging my head against the wall. Basically all I am trying to do is create a view that gets main window as a subview. I know that if self.view is not defined then the loadView method is supposed to be called, and everything is supposed to be set up there. Here is the current state of my code:

The delegate:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    StartMenuViewController *aViewController = [[StartMenuViewController alloc] init];
    self.myViewController = aViewController;
    [aViewController release];

    UIView *controllersView = [myViewController view];

    window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [window setBackgroundColor:[UIColor redColor]];

    [window addSubview:controllersView];
    [window makeKeyAndVisible];
}

The view controller:

- (id)init {
    if (self = [super init]) {
        self.title = @"Start Menu";
    }
    return self;
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    UIView *startView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    [startView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
    [startView setBackgroundColor:[UIColor greenColor]];
    self.view = startView;
    [startView release];
}

Thanks for the help in advance!

A: 

I would strongly recommend you use interface builder for at least your initial Window/View.

If you create a new project in XCode you should be able to select from one of many pre-defined iPhone templates that come with everything setup.

Andrew Grant
I actually have already tried out Interface Builder, for a few initial programs that I wrote. This code actually resulted when I was trying to create a navigation controller, and I added the view and it wouldn't show up. So I tried to see if I could just add to the main window(code above), no dice
Alex
There's nothing wrong with creating your window, views and view controllers programmatically. Doing it yourself helps you to understand how the framework is organized. Plus, I've found the code in the Xcode template apps rather convoluted.
Don McCaughey
I agree that there's nothing wrong, but for someone new to the SDK using the templates is a better way to begin.
Andrew Grant
+2  A: 

Are you sure that you're inheriting from UIViewController and not overriding the implementation of - (UIView*)view?

EDIT: More info:

UIViewController has a special implementation of the "-(UIView*) view" message so that when it's called, the loadView method is called if the view member variable is not set. So, if you provide an implementation of "- (id)view" in your subclass, (or a property named view) it will break the auto-calling of "- loadView".

Jesse Rusak
This is the header for the view controller#import <UIKit/UIKit.h>@interface StartMenuViewController : UIViewController { UIView *view;}@property (nonatomic,retain) UIView *view;@end
Alex
Are you using @synthesize view? If you are, that's the problem. You shouldn't have to declare or implement the view @property
Jesse Rusak
(You also shouldn't have the view member, but I don't think that's related. It may cause problems later, though.)
Jesse Rusak
The instance variable view and the corresponding property named view are declared in UIViewController. By declaring them again, you're hiding the ones in the base class.
Don McCaughey
Wow, I knew it must have been something this simple, though I would have never caught this error on my own. I had declared the @synthesize view;. Thank you, thank you!
Alex
A: 

Unless I am reading this wrong, you did not associate any view with the the controller's view property like this

myViewController.view = controllersView;

So as far as Cocoa is concerned the view you are setting in the window has no controller to call loadView on. loadView is a View controller, not view, method. The view you assign to the window is not associated with any view controller. So your view controller loadView method is never called. Get it? The view you are trying to display, has no view controller associated with it.

When you use interface builder to create views you can link the UIView object you created in IB to the view property in the controller in IB which the framework automatically

But if not done in IB you have to set it

Jeff G
A: 

Just to document a "loadView is not called" case:

I wrote a 2 UITableViewController(s) to handle detail data for a master ViewController. Since the devil was in #2, I made a simple UITableViewController for #1, and referenced it in the XIB for the "master" ViewController.

When I was done with #2, I could simply copy the code to #1, remove the complicated code, and go on with life.

But to my dismay and several days work, no matter what I did, viewLoad was not being called for my simple #1 UITableViewController.

Today I finally realised that I was referencing the UITableViewController in the XIB to the master ViewController program. - and of course, loadView was never being called.

Just to help some other dork that makes the same mistake....

Best Regards, Charles

Yggdrasil