tags:

views:

600

answers:

1

Hi all,

I'm new to iPhone programming, and I'm trying to make a simple program without a NIB. I have worked through some NIB tutorials, but I'd like to try some things programmatically.

My code loads without errors, makes the status bar black, and makes the background white. But, I don't think I'm loading my view with a label correctly after that. I presume I'm doing something just fundamentally wrong, so if you could point me in the right direction, I'd appreciate it. I think if I can get the label to show, I'll get some understanding. Here's my code:

//helloUAppDelegate.h
#import <UIKit/UIKit.h>
#import "LocalViewController.h"

@interface helloUAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    LocalViewController *localViewController;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) LocalViewController *localViewController;

@end


//helloUApDelegate.m
#import "helloUAppDelegate.h"

@implementation helloUAppDelegate

@synthesize window, localViewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {   
    application.statusBarStyle = UIStatusBarStyleBlackOpaque;
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    if (!window) {
        [self release];
        return;
    }
    window.backgroundColor = [UIColor whiteColor];

    localViewController = [[LocalViewController alloc] init];

    [window addSubview:localViewController.view];

    // Override point for customization after application launch
    [window makeKeyAndVisible];
}


//LocalViewController.h
#import <UIKit/UIKit.h>

@interface LocalViewController : UIViewController {
    UILabel *myLabel;   
}

@property (nonatomic, retain) UILabel *myLabel;

@end


//LocalViewController.m
#import "LocalViewController.h"

@implementation LocalViewController

@synthesize myLabel;

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];      
    self.myLabel.text = @"Lorem...";
    self.myLabel.textColor = [UIColor redColor];
}

- (void)dealloc {
    [super dealloc];
    [myLabel release];
}
+2  A: 

Add your label to your LocalViewController's view:

- (void)loadView {
    [super loadView];
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];      
    self.myLabel.text = @"Lorem...";
    self.myLabel.textColor = [UIColor redColor];
    [self addSubview:self.myLabel];
    [self.myLabel release];     // since it's retained after being added to the view
}
Marco Mustapic
Add thin line gives me a warming: 'LocalViewController' may not respond to '-addSubView'
Andrew Johnson
Oh, thanks. This works if I do: [self.view addSubview:self.myLabel];
Andrew Johnson