14

我正在尝试以这种方式从应用程序委托 .m 加载 UIStoryboard:

- (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;
}

这段代码有什么问题??任何想法?

4

4 回答 4

33
- (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;
}

尝试这个。我认为缺少设置根视图控制器并使窗口可见。

于 2012-09-23T16:02:57.587 回答
4

对于 Swift 4.2 及更高版本。

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
}
于 2018-08-16T09:30:40.223 回答
2

从您的代码中,我看到 UIWindow 对象未初始化。

如果您没有在App-Info.plist.

此外,使 Window 键可见。请更改您的代码,如下所示:

- (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;
}
于 2013-01-26T04:06:06.757 回答
-2

现在这种方式行不通了。

在构建设置 plist 中,为 Main Storyboard(或类似的东西)添加了一个条目。您在 applicationDidFinishLoading 中需要的只是返回 YES;

如果您开始一个新项目并勾选了情节提要,您可以看到确切的条目。

当它在那里时,您不需要加载初始视图,因为它从情节提要文件中获取它(是初始视图标志)。

高温高压

于 2012-09-23T15:35:40.620 回答