向UIWindow添加UIViewcontroller的视图不会显示

时间:2013-11-02 07:48:28

标签: ios iphone uiview uiviewcontroller uiwindow

我正在尝试使用以下代码查看视图,其中我使用的是storyboard但是我没有制作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;

1 个答案:

答案 0 :(得分:1)

  1. 首先将storyboard id分配给故事板中的abcViewController,例如“firstView”。
  2. 在app delegate
  3. 中导入viewController
  4. 在故事板中,取消选中第一个视图控制器中的“是初始视图控制器”属性。
  5. 在应用的info.plist中,删除“主故事板文件基本名称”的值。
  6. 实例化故事板,创建窗口对象并在应用委托的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;
    }
    
相关问题