【问题标题】:Push segue animation not working推动转场动画不起作用
【发布时间】:2017-12-13 19:01:12
【问题描述】:

我正在开发一个用 Objective(只有一个类)编写的应用程序,而该应用程序的其余部分是用 Swift 编写的。在我的应用中,我有 3 个故事板,这些故事板相应地有它们的 viewControllers

问题:有时我使用下面的代码更改rootViewController,之后推送动画停止工作。

// constant to share globally 
let kApplicationDelegate = UIApplication.shared.delegate as? AppDelegate

let controller = UIStoryboard().getControllerInstance(storyBoardName: "Autograph", identifire: "loginSuccessVC")
kApplicationDelegate?.window?.rootViewController = controller

【问题讨论】:

  • 你检查过controller类型吗?它是你实例化的同一个视图控制器吗?如果没有尝试添加as! yourviewcontroller
  • 为什么要设置 rootViewController?您可以直接从当前的导航控制器推送。
  • loginSuccessVC 的类型是什么?
  • @iAnurag,有时故事板在导航时是不同的。
  • @AravindAR,故事板。

标签: ios objective-c swift storyboard


【解决方案1】:

如何正确进行Push Segue过渡

我知道现在回复这个问题有点晚了!但迟到总比没有好!

首要任务:
要使 Push 转换正常工作,您必须让 UINavigationController 将您当前的 ViewController 托管为 rootViewController。这到底是什么意思?

示例:
有两个或多个 ViewController(此处为 ViewController == VC)A、B 等。如果您当前在 VC A 中,并且您想移动到 VC B 对 VC A 中的按钮操作执行 Push(或 Detail)转换,请记住您的 VC A 必须是 UINavigationController 的 RootViewController。

[  UI         ]                          [     ]            [     ]
[  Navigation ]-- RootViewController ==O>[  A  ]--- Push -->[  B  ] 
[  Controller ]                          [     ]            [     ]

否则它不起作用。它会显示为当前模态转换。

到目前为止,我已经认可了两种方法来实现这一目标。一种是纯程序化的,另一种是结合使用 StoryBoard 和 Code。

以编程方式:
为了便于解释,我将在UIViewController 堆栈的开头嵌入我的UINavigationController。为了实现这一点,我们必须对AppDelegatedidFinishLaunchingWithOptions launchOptions 方法做一点改动。它应该如下所示:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let initialVCIdentifier = "AViewController" // A ViewController **
    let firstVC = storyboard.instantiateViewController(withIdentifier: initialVCIdentifier)
    let nav = UINavigationController(rootViewController: firstVC) // Assigning the A VC as the RootViewController of the NavigationController
    window?.rootViewController = nav  // Look for the *** Note bellow
    window?.makeKeyAndVisible()

    return true
}

** 您必须在 InterfaceBuilder 中为 VC 表示 ViewController 标识符。我假设您的故事板名称是 Main.storyboard。
*** 如果您没有为初始 View 控制器执行此操作,只需将此 UINavigationController 显示为 VC 工作流路径中的模态 VC

在 VC A 中的按钮操作中,它应该如下所示:

@IBAction func TestBtnAct(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "BViewController") as UIViewController
    navigationController?.pushViewController(vc, animated: true)
}

就是这样!我想如果你已经按照我上面提到的那样尝试过,它应该已经很神奇了。

故事板和代码携手并进:
这意味着您的 VC A 已经由故事板设计中的UINavigationController 托管。在这种情况下,您不需要以编程方式创建UINavigationController 并将VC A 嵌入为RootViewController。只需执行上述 Button 操作就足够了。

希望这对那里的人有所帮助。这是一个sample project I have done and uploaded in GitHub,供大家学习。

干杯!

【讨论】:

  • 我的荣幸!但是你有没有试过它是否适合你?
  • 那是一个老问题,之前已经解决了,问题是appDelegate 对象。 :)
【解决方案2】:

当您更改rootViewController 时,请尝试将navigationController 设置为根而不是navigationControllerrootViewController

假设您有一个 viewController,我们将其命名为 VCAUINavigationController Nav1 的 rootViewController。然后不要将VCA 设为rootViewControllerkApplicationDelegate?.window?。尝试将Nav1 设为rootViewControllerkApplicationDelegate?.window?

kApplicationDelegate?.window?.rootViewController = Nav1

动画不工作,因为它可能不是正在发生的push。您推送的ViewController 中的self.navigationController 可能为零。

【讨论】:

  • @vaibhav 上述方法是否解决了您的问题?
  • @vaibhav 现在有什么错误?动画还是不行吗?
  • 只是为了确认在设置ApplicationDelegate?.window?.rootViewController时必须正确设置导航控制器的viewController标识符??
猜你喜欢
  • 2012-10-25
  • 2015-03-27
  • 2023-03-05
  • 2010-11-27
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
  • 2011-12-19
  • 1970-01-01
相关资源
最近更新 更多