0

このコードをエラーなしでコンパイルしましたが、 welcomeUIViewを表示できません。

はい、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;
}

ビュー.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];
}
4

2 に答える 2

0

バグは、ヘッダーのようにwindow宣言さstaticれているため、各翻訳単位には独自の個別のwindow.

AppDelegate.mm 内で変数を設定しましたwindowが、view.mm にwindowはまだ nil である別のものがあります。共有windowする場合は、ヘッダーで を宣言externしてから、AppDelegate.mm 内で定義する必要があります。

また、UIViewController をサブクラス化し、loadView.

于 2013-07-15T19:32:27.803 に答える