2013-07-15 2 views
0

이 코드는 오류없이 컴파일되었지만 welcomeUIView을 표시 할 수 없습니다.RootViewController는 대리자 외부에서 액세스 할 때 하위 뷰를 추가하지 않습니다.

예, 안으로 들어가면 didFinishLaunchingWithOptions이 표시됩니다. 그런데 왜 내가 원하는대로 할 수 없습니까? 여기

콘솔 밖으로 :

didFinishLaunchingWithOptions 
view() 
finished 

코드 :

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 for customization after application launch. 
window.backgroundColor = [UIColor whiteColor]; 
[window makeKeyAndVisible]; 

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

return YES; 
} 
,174,

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

답변

0

window은 헤더에 static으로 선언되어 있으므로 각 번역 단위마다 고유 한 window이 표시됩니다.

window 변수를 AppDelegate.mm에 설정했으나 view.mm에서 window이 여전히 0 인 변수가 있습니다. window을 공유하려는 경우 헤더에 extern을 선언 한 다음 AppDelegate.mm 내에 정의해야합니다.

또한 UIViewController를 서브 클래스 화하고 loadView을 재정 의하여 자신의 뷰 계층 구조를 설정하도록해야합니다.

0

쉽게 루트 뷰 컨트롤러에서보기를 추가 할 수 있습니다. 당신이 상태 표시 줄 또는 탐색 모음이있는 경우 그러나 화면 전체를 커버하지, viewDidLoad

-(void)viewDidLoad { 
    [super viewDidLoad]; 

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

이 의지에 작업을 수행합니다. 대신 코드에서 CGRect를 사용하여 self.view.window에보기를 추가 할 수 있습니다.

관련 문제