2009-06-19 66 views
2

我是iPhone新手,我试图做一个没有NIB的简单程序。我已经通过一些NIB教程,但我想尝试一些编程方式。试图让UILabel在视图中显示,没有NIB

我的代码加载没有错误,使状态栏变黑,并使背景变成白色。但是,我认为之后我没有正确加载标签。我想我正在做一些根本性错误的事情,所以如果你能指出我正确的方向,我会很感激。我想如果我可以拿到标签来展示,我会理解的。这里是我的代码:

//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]; 
} 

回答

3

您的标签添加到您的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

添加细线给了我一个温暖:“LocalViewController”不能为“-addSubView” – 2009-06-19 20:34:14