4

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

- (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 视图之上,即在窗口上,因此尽管窗口呈现的视图发生变化,它仍将始终存在。我不想只在这里显示标签。

我得到了答案

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

谢谢大家指点。

4

2 回答 2

4

只需删除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];
于 2012-11-22T10:45:54.103 回答
3

将标签添加到 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];
于 2012-11-22T10:44:05.970 回答