Question

I am trying to see a view with the following code where I am using storyboard however I did not make the uiviewcontroller's scene initial view controller. This is the code I wrote in didFinishLaunchingWithOption of my AppDelegate.

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;
Was it helpful?

Solution

  1. First assign storyboard id to the abcViewController in storyboard eg."firstView".
  2. Import the viewController in app delegate
  3. In the storyboard, uncheck the "Is initial View Controller" attribute from the first view controller.
  4. In the app's info.plist, remove the value of "Main storyboard file base name".
  5. Instantiate the storyboard, create the window object and set initial view controller in the app delegate's application:didFinishLaunchingWithOptions: method

    - (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;
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top