我想从appDelegate设置初始视图控制器.我找到了一个非常好的答案,然而它是在目标C中,而我在快速实现同样的事情上遇到了困难.

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依赖于使用条件语句满足的某些条件.

推荐答案

Xcode11和SceneDelegate注意:

从Xcode11开始,由于SceneDelegates,您很可能不应该在AppDelegate中执行该操作.取而代之的是从SceneDelegate开始.有关这方面的更多信息,请参阅this other answer


旧答案:

我用这个线索帮助我把目标C转换成了SWIFT,它工作得很好.

Instantiate and Present a viewController in Swift

100代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
    let initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginSignupVC")
    
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
    
    return true
}

100代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()

    return true
}

Ios相关问答推荐

创建带有Flutter 翼的会所头像

在iOS中禁用URLSession自动重试机制

由于代码签名时出错,无法在iOS设备上发布iOS Maui Build

IFrame内容拒绝仅在iOS浏览器上加载-即使发布了内容-安全-策略(CSP)框架-祖先指令

CIKernel的Metal Shader裁剪问题

iOS应用启动时崩溃问题解决方法-NO_CRASH_STACK-DYLD 4个符号丢失

Flutter iOS 构建失败:DVTCoreDeviceEnabledState_Disabled

在我的 M1 上出现 Architecture arm64 问题,因此我无法运行我的旧项目

UIView 倒置旋转时的zoom 问题

一对多关系 Firebase 实时数据库(嵌入数百万条 comments )

NSOperation 有延迟 - 它是异步的还是同步的?

react 在 android 上运行的本机应用程序,但在 iOS 上出现问题,详细信息在描述中共享

如何让我的 iOS 应用程序与 api 连接?

Flutter:无法构建 iOS 应用程序ARCHIVE FAILED,为设备归档时遇到错误

iOS 中的 [Class new] 和 [[Class alloc] init] 有什么区别?

在代码生成的 UIView 上绘制 UIBezierPath

使用 convertPoint 获取父 UIView 内的相对位置

SwiftUI NavigationView navigationBarTitle LayoutConstraints 问题

如何进行键值观察并在 UIView 的框架上获取 KVO 回调?

如何为 NSDate 添加一个月?