我想要从应用程序委托中设置初始的视图控制器。我找到了一个非常好的答案,然而它是在目标C中,而我在swift中实现同样的事情时遇到了困难。
Programmatically set the initial view controller using Storyboards
- (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;
}
有人能帮上忙吗?
我希望初始的Viewcontroller依赖于使用条件语句满足的某些条件。
发布于 2014-11-05 18:07:13
尝尝这个。例如:您应该使用UINavigationController
作为初始视图控制器。然后,您可以从故事板中将任何视图控制器设置为root。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = storyboard.instantiateInitialViewController() as UINavigationController
let rootViewController = storyboard.instantiateViewControllerWithIdentifier("VC") as UIViewController
navigationController.viewControllers = [rootViewController]
self.window?.rootViewController = navigationController
return true
}
发布于 2019-10-16 20:41:19
对于新的Xcode11.xxx和Swift 5.xx,其目标设置为iOS 13+。
对于新的项目结构,AppDelegate不需要对rootViewController做任何事情。
这里有一个新的类来处理窗口(UIWindowScene)类-> 'SceneDelegate‘文件。
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = // Your RootViewController in here
self.window = window
window.makeKeyAndVisible()
}
}
发布于 2014-11-05 18:34:40
如果你没有使用故事板,你可以试试这个。
var window: UIWindow?
var initialViewController :UIViewController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
initialViewController = MainViewController(nibName:"MainViewController",bundle:nil)
let frame = UIScreen.mainScreen().bounds
window = UIWindow(frame: frame)
window!.rootViewController = initialViewController
window!.makeKeyAndVisible()
return true
}
https://stackoverflow.com/questions/26753925
复制相似问题