我正试图以这种方式从app delegate .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;
}
这段代码有什么问题?任何的想法?
答案 0 :(得分:32)
- (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;
}
试试这个。我认为缺少设置根视图控制器并使窗口可见。
答案 1 :(得分:2)
从您的代码中我看到UIWindow对象未初始化。如果您没有在App-Info.plist中提及任何故事板名称,则必须对其进行初始化。此外,使窗口键和可见。请更改您的代码,如下所示:
- (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;
}
答案 2 :(得分:2)
对于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
}
答案 3 :(得分:-1)
现在这种方式不起作用。
在构建设置plist中,为Main Storyboard添加一个条目(或类似的东西)。 applicationDidFinishLoading中您需要的只是返回YES;
如果你开始一个带有故事板的新项目,你可以看到确切的条目。
当它在那里时你不需要加载初始视图,因为它从故事板文件(是初始视图标志)中获取它。
HTH