ios - 无法关闭模态视图 Controller

标签 ios swift xcode

无法关闭模态视图 Controller 并返回到 Root View Controller 。动画确实显示但仍然弹出当前 View Controller 。

我在不使用 Storyboard 的情况下开发应用程序,我想关闭当前的模态视图 Controller 并返回到 Root View Controller 。

有什么正确的方法可以做到这一点吗?

我的根 Controller 是导航 Controller ,而我的模态视图 Controller 是UIViewController。这是不工作的根本原因吗?

模态视图 Controller (PlayerViewController)

func handleBack() {
    self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
}

HomeController 中的 FeedCell.swift (FeedCell.swift)

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let playerViewController = PlayerViewController()
    playerViewController.modalPresentationStyle = .overCurrentContext
    self.window?.rootViewController?.present(playerViewController, animated: true, completion: nil)
}

AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let layout = UICollectionViewFlowLayout()
    window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))

    UINavigationBar.appearance().barTintColor = UIColor.rgb(red: 230, green: 32, blue: 31)

    // get rid of black bar underneath navbar
    UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().setBackgroundImage(UIImage(),for: .default)

    application.statusBarStyle = .lightContent

    let statusBarBackgroundView = UIView()
    statusBarBackgroundView.backgroundColor = UIColor.rgb(red: 194, green: 31, blue: 31)

    window?.addSubview(statusBarBackgroundView)
    window?.addConstraintsWithFormat(format: "H:|[v0]|", views: statusBarBackgroundView)
    window?.addConstraintsWithFormat(format: "V:|[v0(20)]", views: statusBarBackgroundView)

    return true
}

“Tapped”确实显示了但解散不起作用 enter image description here

最佳答案

如果你想关闭当前显示为模态的 View Controller ,你应该调用显示 View Controller 的 View Controller 并告诉它关闭显示的 View Controller :

self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)

presentingViewController :

The view controller that presented this view controller.

关于ios - 无法关闭模态视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40715226/

相关文章:

ios - viewController 没有被正确删除?

ios swift 确实选择了行来检查字符串是否相等

swift - SwiftUI 的 List 如何更新选择绑定(bind)?

objective-c - 使用 OCUnit 的单元测试示例

ios - SKNode 在 SKConstraint 范围内跟随另一个 SKNode

ios - 有没有办法阻止 UICollectionView 重复使用单元格?

objective-c - 访问 plist 数据

ios - AFNetworking 2.0 POST 问题 |替换已弃用的 multipartFormRequestWithMethod :path:parameters

ios - 解析: Foreground Push Open Tracking

iOS SimpleDB 按日期选择