Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIStoryboard load from app delegate

I am trying to load a UIStoryboard from the app delegate .m in this way:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storybord instantiateInitialViewController];
    [self.window addSubview:vc.view];

    return YES;
}

What is the problem with this code?? any idea?

like image 914
Alessandro Avatar asked Sep 23 '12 15:09

Alessandro


People also ask

What is an app delegate?

So an app delegate is an object that the application object can use to do certain things like display the first window or view when the app starts up, handle outside notifications or save data when the app goes into the background.


2 Answers

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
   UIViewController *vc =[storyboard instantiateInitialViewController];

   // Set root view controller and make windows visible
   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   self.window.rootViewController = vc;
   [self.window makeKeyAndVisible];

   return YES;
}

Try this. I think is missing set root view controller and make windows visible.

like image 144
martinezdelariva Avatar answered Nov 13 '22 23:11

martinezdelariva


For Swift 4.2 and higher.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: Bundle.main)
    let viewController = storyboard.instantiateInitialViewController()
    window?.rootViewController = viewController
    window?.makeKeyAndVisible()
    return true
}
like image 37
Eugene Avatar answered Nov 14 '22 01:11

Eugene