0

ストーリーボードを使用している次のコードでビューを表示しようとしていますが、uiviewcontroller のシーンの初期ビュー コントローラーを作成していません。これは、AppDelegate の didFinishLaunchingWithOption に記述したコードです。

UIWindow* window = [UIApplication sharedApplication].keyWindow;  
abcViewController *controller = [[abcViewController alloc]init];  
UIView *redView = [[UIView alloc] initWithFrame: CGRectMake ( [[UIScreen mainScreen] bounds].origin.x+30, [[UIScreen mainScreen] bounds].origin.y+30, 260, 400)];
[redView setBackgroundColor:[UIColor redColor]];
UIView *greenView = [[UIView alloc] initWithFrame: CGRectMake ( redView.frame.origin.x + 10.0f, redView.frame.origin.y + 10.0f, 180, 320)];
[greenView setBackgroundColor:[UIColor greenColor]];
[redView addSubview:greenView];
[controller.view addSubview:redView];
window.rootViewController = controller;
[window makeKeyAndVisible];
return YES;
4

1 に答える 1

1
  1. まずstoryboard id、ストーリーボードの abcViewController に割り当てます (例:「firstView」)。
  2. アプリ デリゲートで viewController をインポートする
  3. ストーリーボードで、最初のビュー コントローラーから「Is initial View Controller」属性のチェックを外します。
  4. アプリのinfo.plistで、「メイン ストーリーボード ファイルのベース名」の値を削除します。
  5. ストーリーボードをインスタンス化し、ウィンドウ オブジェクトを作成し、アプリ デリゲートのapplication:didFinishLaunchingWithOptions:メソッドで初期ビュー コントローラーを設定する

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        // Override point for customization after application launch.
    
        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
        TestViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"firstView"];
    
        UIView *redView = [[UIView alloc] initWithFrame: CGRectMake ( [[UIScreen mainScreen] bounds].origin.x+30, [[UIScreen mainScreen] bounds].origin.y+30, 260, 400)];
        [redView setBackgroundColor:[UIColor redColor]];
        UIView *greenView = [[UIView alloc] initWithFrame: CGRectMake ( redView.frame.origin.x + 10.0f, redView.frame.origin.y + 10.0f, 180, 320)];
        [greenView setBackgroundColor:[UIColor greenColor]];
        [redView addSubview:greenView];
        [controller.view addSubview:redView];
    
        self.window.rootViewController = controller;
        [self.window makeKeyAndVisible];            
    
        return YES;
    }
    
于 2013-11-02T08:32:05.257 に答える