我正在使用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];
感谢所有人提供指示。
答案 0 :(得分: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];
答案 1 :(得分: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];