我想从appdelegate设置初始viewcontroller.我找到了一个非常好的答案,但它是在Objective C中,我很难在swift中实现同样的功能.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
有人能帮忙吗?
我希望初始Viewcontroller依赖于使用条件语句满足的某些条件.
考虑以下场景:我有一个基于故事板的应用程序.我将一个ViewController对象添加到storyboard中,将此ViewController的类文件添加到项目中,并在IB身份检查器中指定新类的名称.现在我将如何从AppDelegate以编程方式引用此ViewController?我已经使用相关类创建了一个变量并将其转换为IBOutlet属性,但我没有看到任何方法能够在代码中引用新的ViewController - 任何尝试按住Ctrl键拖动连接都不起作用.
即在AppDelegate中我可以像这样到达基本的ViewController
(MyViewController*) self.window.rootViewController
Run Code Online (Sandbox Code Playgroud)
但故事板中包含的任何其他ViewController怎么样?