4

I am using Xcode 4.5 and iOS 6.0, I have selected the default single view template from the xcode, I just want to add the label on the application window but I am unable to do so. Please correct me.

- (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 - I want my label on top of my ViewController view i.e. on the window so it will be always there despite changes in the views presented by window. I dont want to only show the label here.

I got the answer

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

Thanks all for giving the pointers.

2 Answers 2

4

Just Remove the 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;
}

if you dont want to only show the label here then use like below.

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


[self.Viewcontroller.view addSubview:label];
1
  • I want my label on top of my ViewController view i.e. on the window so it will be always there despite changes in the views presented by window. I dont want to only show the label here.
    – mangesh
    Nov 22, 2012 at 10:52
3

Add the label to self.window.rootViewController.view instead of 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];
2
  • 1
    He wants to add in application window not in viewcontroller's view. Nov 22, 2012 at 10:46
  • @pbibergal: I want the that label to be on top of my root view controller i.e. on window so it will be visible on any subsequent views that will be presented in the window. If I assign the root view controller as label then how I can attach my ViewController view.
    – mangesh
    Nov 22, 2012 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.