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. 在情节提要中,取消选中第一个视图控制器中的“是初始视图控制器”属性。
  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 回答