UIStoryboard load from app delegate

风格不统一 提交于 2019-12-19 05:17:38

问题


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?


回答1:


- (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.




回答2:


From your code I see UIWindow Object is not initialised. You have to initialise it if you don't mention any Storyboard Name in App-Info.plist. Also, make the Window key and visible. Please change your code as displayed below:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
UIViewController *vc =[storybord instantiateInitialViewController];
[self.window addSubview:vc.view];
[self.window makeKeyAndVisible];
return YES;
}



回答3:


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
}



回答4:


It doesn't work this way now.

In the build settings plist there is add an entry for Main Storyboard (or something like that). The all you need in the applicationDidFinishLoading is return YES;

If you start a new project with storyboards ticked you can see the exact entry.

When that's in there you don't need to load the initial view as it takes it from the storyboard file (Is Initial View flag).

HTH



来源:https://stackoverflow.com/questions/12553621/uistoryboard-load-from-app-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!