AppDelegate UIWindow addSubView in different viewController

杀马特。学长 韩版系。学妹 提交于 2019-12-10 13:58:16

问题


I am trying to add a UILabel in UIWindow of AppDelegate from a UIViewController. This is how I am doing this:

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 am seeing is grey screen but no label. Where I might be going wrong?


回答1:


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

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

2) While you should be able to add the label to the window, its somewhat unorthodox to do so. In any case, try adding the label to the viewController's view and see if that works, and if so, and you really want to add it to the window (for some reason), then add a comment here:

[self.view addSubview:abcd];

If you still cannot see the label its likely that there is an issue with the view controller. Did you define anything in the nib - any element that should be visible at launch ? If not then add something just so you can be sure the view is in fact getting loaded. [One trick I use is to set the background color of views to red or blue, so I can see that in fact they got loaded.]




回答2:


You must not add UILabel to UIWindow, you should add to UIViewController. Change this line:

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

for this:

[self.view addSubview:abcd];



回答3:


revers the order to

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

Then

try adding to the view first not to the window using following code

[self.view addSubview:abcd];

If this does not show your label then the view controller is not getting loaded.

If so then check the property of your xib file.

remove the window alloc line and check the hook up for your window in mainwindow.xib file if the hoockup is not correct then it will not load the view.




回答4:


Maybe your view controller's view is covering the one you added. Add the new view to the view controller's view instead:

[self.view addSubview:abcd];



回答5:


Try this after adding UILabel in UIWindow

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


来源:https://stackoverflow.com/questions/12176666/appdelegate-uiwindow-addsubview-in-different-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!