从应用程序委托在应用程序窗口添加标签

我正在使用Xcode 4.5和iOS 6.0,我已经从xcode中select了默认的单一视图模板,我只是想在应用程序窗口上添加标签,但我无法这样做。 请纠正我

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)]; label.text = @"Test"; [label setBackgroundColor:[UIColor whiteColor]]; [self.window addSubview:label]; [self.window bringSubviewToFront:label]; [self.window makeKeyAndVisible]; return YES; } 

PS – 我想我的标签在我的ViewController视图顶部,即在窗口,所以它将永远存在尽pipe窗口提出的意见的变化。 我不想只在这里显示标签。

我得到了答案

 [self.window.rootViewController.view addSubview:label]; 

感谢所有的指示。

只需删除RootviewController

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)]; label.text = @"Test"; [label setBackgroundColor:[UIColor whiteColor]]; [self.window addSubview:label]; [self.window bringSubviewToFront:label]; [self.window makeKeyAndVisible]; return YES; } 

如果你不想在这里只显示标签,然后像下面一样使用。

 self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.Viewcontroller.view addSubview:label]; 

将标签添加到self.window.rootViewController.view而不是self.window

 UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)]; label.text = @"Test"; [label setBackgroundColor:[UIColor whiteColor]]; [self.window.rootViewController.view addSubview:label];