2009-06-19 5 views
2

Je suis nouveau à la programmation iPhone, et j'essaye de faire un programme simple sans NIB. J'ai travaillé sur des tutoriels NIB, mais j'aimerais essayer certaines choses par programmation.Essayer d'obtenir un UILabel à afficher dans une vue, sans un NIB

Mon code se charge sans erreur, noircit la barre d'état et rend le fond blanc. Mais, je ne pense pas que je charge ma vue avec une étiquette correctement après cela. Je présume que je fais quelque chose de fondamentalement faux, alors si vous pouviez me diriger dans la bonne direction, j'apprécierais. Je pense que si je peux montrer l'étiquette, je vais comprendre. Voici mon 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]; 
} 

Répondre

3

Ajoutez votre étiquette à votre vue de LocalViewController:

- (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 
} 
+0

Ajouter une ligne mince me donne un réchauffement: 'LocalViewController' ne peut pas répondre à '-addSubView' –

+1

Oh, merci. Cela fonctionne si je le fais: [self.view addSubview: self.myLabel]; –

Questions connexes