AppDelegate UIWindow addSubView in different viewController

I am trying to add a UILabel to a UIWindow from an AppDelegate from a UIViewController. Here is how I do it:

AppDelegate Code :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
    }



    [self.window makeKeyAndVisible];

    self.window.rootViewController = self.viewController;


    return YES;
}

ViewController Code :

- (void)viewDidLoad
{

    UILabel *abcd=[[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 200.0, 40.0)];

    abcd.text=@"loading...";

    abcd.backgroundColor=[UIColor clearColor];

    [[[[UIApplication sharedApplication] delegate] window] addSubview:abcd];

    [super viewDidLoad];


}  

But all I see is a gray screen, but without a label. Where can I be wrong?

+5
source share
5 answers

1) I suggest you change the order of the last two statements of the delegate:

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

2) While you need to add a shortcut to the window, it is somewhat unorthodox to do this. In any case, try adding a label to the viewController and see if it works, and if so, and you really want to add it to the window (for some reason), add a comment here:

[self.view addSubview:abcd];

, , . - nib - , ? , - , , . [ , , , , , .]

+1

UILabel UIWindow, UIViewController. :

[[[[UIApplication sharedApplication] delegate] window] addSubview:abcd];

:

[self.view addSubview:abcd];
+3

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

,

[self.view addSubview:abcd];

, .

If so, check the property of your xib file.

delete the window selection line and check the connection to your window in the mainwindow.xib file if hoockup is incorrect and it will not load the view.

+2
source

Perhaps your kind of controller controller covers the one you added. Instead, add a new view to the view controller's view:

[self.view addSubview:abcd];
0
source

Try this after adding UILabel to UIWindow

  [[[[UIApplication sharedApplication] delegate] window] makeKeyAndVisible];
0
source

All Articles