简体   繁体   English

RootViewController在其委托外部访问时不会添加子视图

[英]RootViewController won't add subview when accessed outside its delegate

I have this code compiled with no error, but I can't make welcomeUIView to be displayed. 我已经编译了这段代码,没有错误,但是我无法显示welcomeUIView

Yes, when I move it inside didFinishLaunchingWithOptions it will show up. 是的,当我将它移入didFinishLaunchingWithOptions内部时,它将显示出来。 But why I can't make it the way I want? 但是为什么我不能按照自己的方式做呢?

Here is the console out: 这是控制台:

didFinishLaunchingWithOptions
view()
finished

The code: 编码:

AppDelegate.h AppDelegate.h

#import <UIKit/UIKit.h>
static UIWindow *window;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end 

AppDelegate.mm AppDelegate.mm

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
std::cout<<"didFinishLaunchingWithOptions"<<std::endl;
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = [[UIViewController alloc] init];

view *v = new view();
v->set();

// Override point for customization after application launch.
window.backgroundColor = [UIColor whiteColor];
[window makeKeyAndVisible];

std::cout<<"finished"<<std::endl;

return YES;
}

view.mm view.mm

#include "view.h"
#include "AppDelegate.h"
#include <UIKit/UIKit.h>
#include <iostream>

void view::set()
{
std::cout<<"view()"<<std::endl;

UIView *welcomeUIView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[welcomeUIView setBackgroundColor:[UIColor darkGrayColor]];

[welcomeUIView setHidden:false];
[window.rootViewController.view addSubview:welcomeUIView];
[window.rootViewController.view bringSubviewToFront:welcomeUIView];
}

You can easily add the view in your root view controller. 您可以在根视图控制器中轻松添加视图。 Do it in viewDidLoad viewDidLoad

-(void)viewDidLoad {
    [super viewDidLoad]; 

    UIView *welcomeView = 
          [[UIView alloc] initWithFrame:self.view.bounds];
    welcomeView.backgroundColor = UIColor.darkGrayColor;
    [self.view addSubview:welcomeView];
}

This will, however not cover the whole screen if you have a status bar or navigation bar. 但是,如果您具有状态栏或导航栏,则不会覆盖整个屏幕。 Instead, you can add the view to self.view.window (with the CGRect from your code). 相反,您可以添加到视图self.view.window (与从的CGRect代码)。

The bug is that window is declared as static in your header, so each translation unit will have its own distinct window . 错误是window在标题中被声明为static window ,因此每个翻译单元将有其自己独特的window

You've set the window variable within AppDelegate.mm, but then in view.mm you have a different window which is still nil. 您已经在AppDelegate.mm中设置了window变量,但是在view.mm中,您拥有另一个仍为nil的window If you meant to share window you should declare is extern in your header and then define it within AppDelegate.mm. 如果要共享window ,则应在标头中声明extern ,然后在AppDelegate.mm中定义它。

Also, you should just let your view controller set up its own view hierarchy by subclassing UIViewController and overriding loadView . 同样,您应该通过子类化UIViewController并覆盖loadView ,让视图控制器设置自己的视图层次结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM