c++ - RootViewController won't add subview when accessed outside its delegate -


i have code compiled no error, can't make welcomeuiview displayed.

yes, when move inside didfinishlaunchingwithoptions show up. why can't make way want?

here console out:

didfinishlaunchingwithoptions view() finished 

the code:

appdelegate.h

#import <uikit/uikit.h> static uiwindow *window;  @interface appdelegate : uiresponder <uiapplicationdelegate> @end  

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 customization after application launch. window.backgroundcolor = [uicolor whitecolor]; [window makekeyandvisible];  std::cout<<"finished"<<std::endl;  return yes; } 

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]; } 

the bug window declared static in header, each translation unit have own distinct window.

you've set window variable within appdelegate.mm, in view.mm have different window still nil. if meant share window should declare extern in header , define within appdelegate.mm.

also, should let view controller set own view hierarchy subclassing uiviewcontroller , overriding loadview.


Comments