2017-05-03 25 views
0

我有一個幻燈片菜單,當我按下我的導航欄中的菜單按鈕時顯示它自己。關閉它可以通過點擊背景或菜單本身來完成。在這個視圖中,我有幾個項目,其中一個是用戶配置文件按鈕。當我按下此按鈕時,我希望菜單自行關閉,然後立即打開用戶配置文件視圖控制器。這就是爲什麼我首先調用handleDismiss()然後設置需要打開的視圖Swift - 從幻燈片菜單中打開UIViewController

但是,它一直告訴我視圖不在窗口層次結構中。我知道問題是什麼(視圖堆棧),但我無法讓它正常工作。我應該如何解決這個問題?

RootViewController的 - >的HomeController(的TabBar索引0) - >滑入式菜單 - > UserProfileController

按鈕打開控制器

profileCell.profileButton.addTarget(self, action: #selector(handleUserProfile(sender:)), for: .touchUpInside) 

功能

func handleDismiss() { 
    UIView.animate(withDuration: 0.5) { 
     self.blackView.alpha = 0 

     if let window = UIApplication.shared.keyWindow { 
      self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height) 
     } 
    } 
} 

func handleUserProfile(sender: UIButton) { 
    handleDismiss() 

    let userProfileController = UserProfileController() 
    openProfileController(userProfileController) 
} 

func openProfileController(_ controller: UIViewController) { 
    present(controller, animated: false, completion: nil) 
} 

回答

0

我修好了通過在我的菜單中設置對rootViewController的引用的問題。

的HomeController

let menuLauncher = MenuLauncher() 

func tappedMenu(sender: UIButton) { 
    menuLauncher.showMenu(self) 
} 

MenuController

func MenuLauncher() { 
    self.m_ParentViewController = nil 
} 

func showMenu(_ parentViewController: UIViewController) { 
    self.m_ParentViewController = parentViewController 

    collectionView.dataSource = self 
    collectionView.delegate = self 

    collectionView.register(MenuLauncherImageCell.self, forCellWithReuseIdentifier: cellIdImage) 
    collectionView.register(MenuLauncherInfoCell.self, forCellWithReuseIdentifier: cellIdInfo) 

    if let window = UIApplication.shared.keyWindow { 
     blackView.backgroundColor = UIColor(white: 0, alpha: 0.5) 

     blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss))) 

     window.addSubview(blackView) 
     window.addSubview(collectionView) 

     collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: 350) 

     blackView.frame = window.frame 
     blackView.alpha = 0 

     UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 
      self.blackView.alpha = 1 
      self.collectionView.frame = CGRect(x: 0, y: window.frame.height - 350, width: self.collectionView.frame.width, height: self.collectionView.frame.height) 

     }, completion: nil) 
    } 
} 

func handleDismiss() { 
    UIView.animate(withDuration: 0.5) { 
     self.blackView.alpha = 0 

     if let window = UIApplication.shared.keyWindow { 
      self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height) 
     } 
    } 
} 

func handleUserProfile(sender: UIButton) { 
    handleDismiss() 

    let userProfileController = UserProfileController() 
    self.m_ParentViewController?.present(userProfileController, animated: true, completion: nil) 
}