2016-11-21 215 views
3

无法解除模态视图控制器并返回到根视图控制器。动画确实显示,但仍弹出当前的视图控制器。无法解除模态视图控制器

我正在开发的应用程序没有使用storyboard,我想解雇当前的模式视图控制器和回到根视图控制器。

有没有适当的方法来做到这一点?

我的根控制器是导航控制器虽然我的模式 - 视图 - 控制器是的UIViewController。这是不工作的根本原因吗?

模态视图控制器(PlayerViewController)

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

FeedCell.swift中的HomeController(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 
} 

“抽头”没有显示,但驳回不工作 enter image description here

+5

邮编,没有想象 – Tj3n

+0

@ Tj3n完成编辑 – aznelite89

+0

代码后重新添加按钮,在'模式视图控制器(PlayerViewController) ' – Vinodh

回答

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

试试这个。

+1

布拉沃,像魅力工作 – aznelite89

+0

谢谢,兄弟。有惊人的发展日。 –

1

如果你想关闭那些被提出作为模态当前视图控制器,你应该调用所呈现的视图控制器的一个,并告诉它驳回呈现视图控制器:

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

presentingViewController

所呈现该视图控制器视图控制器。

+0

嗨感谢代码,但它无法正常工作。 – aznelite89

+0

您应该直接从模态VC文件提供的视图控制器调用该调用。 – OhadM

+0

仍然无法工作,仍然无法正常工作。我的根控制器是导航控制器,我的模式视图控制器是UIViewController,它有可能是不工作的根本原因? – aznelite89

0

要消除任何的viewController,您应该使用

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

试试这个:

_ = self.navigationController?.popToRootViewController(animated: true)