无法关闭模态视图控制器

3
无法关闭模态视图控制器并返回到根视图控制器。动画确实显示,但仍会弹出当前视图控制器。
我正在开发应用程序,不使用storyboard,并且希望关闭当前模态视图控制器并返回到根视图控制器。是否有适当的方法可以做到这一点?
我的根控制器是导航控制器,而我的模态视图控制器是UIViewController。这是不起作用的根本原因吗?
模态视图控制器(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

5
邮政编码,不是图片。 - Tj3n
@Tj3n 完成编辑。 - aznelite89
在“模态视图控制器(PlayerViewController)”中添加返回按钮的代码。 - Vinodh
4个回答

5
如果您想关闭当前作为模态窗口呈现的视图控制器,您应该调用呈现该视图控制器的那个视图控制器,并告诉它关闭已呈现的视图控制器:
self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)

presentingViewController

呈现此视图控制器的视图控制器。


你应该直接从呈现的视图控制器中调用该调用,该视图控制器是模态VC文件。 - OhadM
仍然无法工作,仍然无法工作。我的根控制器是导航控制器,我的模态视图控制器是UIViewController,这可能是不工作的根本原因吗? - aznelite89
1
首先,您应该使用Storyboard而不是硬编码所需的操作。看起来您需要在appDelegate中调试窗口类成员。 - OhadM

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

尝试这个。

谢谢,兄弟。祝你有一个令人惊叹的开发日。 - Kent Chong

0

要关闭任何视图控制器,您应该使用

func handleBack() {
        self.dismiss(animated: true, completion: {})
    }

0
尝试这个:
_ = self.navigationController?.popToRootViewController(animated: true)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接