ios - 以编程方式在场景之间进行转换的问题

标签 ios swift segue

我的问题是我无法找到正确的代码来以编程方式在 IOS 中的两个场景之间进行转换。我尝试了一些示例代码,但没有一个按预期工作。

let vc storyboard!.instantiateViewController(withIdentifier: "savedMode") as! SavedModeViewController
        let navigationController = UINavigationController(rootViewController: vc)
        self.present(navigationController, animated: true, completion: nil)

这段代码进入了新的场景,但是后退按钮没有出现在导航栏的左侧,并且场景之间的转换是错误的;有从下到上的动画,而不是从左到右的动画。

let controller = storyboard?.instantiateViewController(withIdentifier: "savedMode") as! SavedModeViewController
        present(controller, animated: true, completion: nil)

这是我尝试过的第二个代码。问题是,使用此代码,导航栏在第二个场景中根本不显示。

我尝试了另一种方法,但不是第二个场景,屏幕只是变黑了。

最佳答案

调用 present 将以模态方式在当前 View Controller 上呈现新的 View Controller 。

您想要的是将新的 View Controller 推送到现有的 UINavigationController 上 pushViewController .

if let savedVC = storyboard?.instantiateViewController(withIdentifier: "savedMode") as? SavedModeViewController, 
   let navigationController = self.navigationController {

    navigationController.pushViewController(savedVC, animated: true)

} else {

    // TODO: Handle unexpected nil occurrences
    print("Error loading SavedModeViewController")

}

关于ios - 以编程方式在场景之间进行转换的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57233252/

相关文章:

iOS CBCentralManager "Turn on Bluetooth"提示和CBCentralManagerOptionShowPowerAlertKey默认值

ios - 推送基于 swift 的应用程序所需的 Xcode 版本

ios - tableview 中的图像加载另一个几秒钟

ios - 从导航 Controller 中分离出来

ios - 今天如何在 iOS/OS X View 扩展中使用 Crashlytics?

ios - ABCreateStringWithAddressDictionary 隐式声明

ios - 工具栏上 UIBarButtonItem 右侧的图像图标 (iOS)

swift - 此 NSPersistentStoreCoordinator 没有持久性存储(设备已锁定)。它无法执行保存操作。专门的静态方法

ios - 为什么我的变量的值在Swift中恢复?

xcode - 使用 Segue 自动加载 SecondViewController