【问题标题】:adding UIViewcontroller's view to UIWindow does not show将 UIViewcontroller 的视图添加到 UIWindow 不显示
【发布时间】:2013-11-02 07:48:28
【问题描述】:

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

【问题讨论】:

    标签: ios iphone uiview uiviewcontroller uiwindow


    【解决方案1】:
    1. 首先将storyboard id 分配给情节提要中的abcViewController,例如“firstView”。
    2. 在应用委托中导入 viewController
    3. 在情节提要中,取消选中第一个视图控制器中的“是初始视图控制器”属性。
    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;
      }
      

    【讨论】:

      猜你喜欢
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多