2016-11-23 78 views
2

我面臨一個問題,因爲有兩種方法我如何顯示我的ViewControllerUIBarButtonItem取決於演示文稿。 Swift3

  1. 第一種方式是performSegue(withIdentifier: "segue", sender: self)

它正常工作,因爲那時我在navigationItem有這樣的後退按鈕:

enter image description here

  • 然後我使用此代碼來呈現與第一種情況相同的ViewController(來自另一個viewController):

    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
        let navVC = storyboard.instantiateViewController(withIdentifier: "navViewController") as! UINavigationController 
        let vc = navVC.topViewController as! ViewController    
        self.present(navVC, animated: true, completion: nil) 
    
  • 但當時我沒有在我的ViewController任何後退按鈕。

    我的問題是:我怎樣才能讓我的backButton (exactly how it is)當我使用這個功能:performSegue(withIdentifier: "segue", sender: self),但添加按鈕(可以看看不同),當我使用此功能:self.present(navVC, animated: true, completion: nil)

    注:在我的情況1我的segue直接連接到ViewController,但在情況2中,我在此UINavigationController中出現UINavigationControllerViewControllerembed in

    編輯:我試過這個代碼,但它始終打印:"1........."

    if self.presentingViewController != nil { 
          print("1..........") 
         } else if self.navigationController?.presentingViewController?.presentedViewController == self.navigationController { 
          return print("2.........") 
         } else if self.tabBarController?.presentingViewController is UITabBarController { 
          return print("3........") 
         } 
    

    而且也是這個代碼打印:"Else.............."

    let isPresentingInAddMealMode = presentedViewController is UINavigationController 
    
    if isPresentingInAddMealMode { 
        print("FirstOption......................") 
    
    } else { 
        print("Else......................") 
    
    } 
    

    如果您需要更多的信息只是讓我知道。 非常感謝。

    +2

    當你提出一個視圖控制器作爲一個模式,你需要自己添加的按鈕navigationItem。 – Sealos

    +0

    我知道,但是當我添加一個按鈕navigationItem我的「<返回」消失,只有我添加的按鈕 –

    回答

    0

    我已經解決了它!我把restorationIdentifier我的根navigationController,然後我就檢查是否是navigationControllerrestorationIdentifier這樣的:

    if self.navigationController?.restorationIdentifier == "navViewController"{ 
         let leftItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(goBack)) 
         self.navigationItem.leftBarButtonItem = leftItem //Adds item if id is navViewController 
        }else{ 
         self.navigationItem.leftBarButtonItem = nil //removes it and keeps "<Back" 
        } 
    
    0

    您需要檢查presentedViewController並添加後退按鈕,如下所示。

    if ([self presentedViewController]) { 
         // add your back button item here 
        } 
    
    +0

    我試過這個選項,但它總是添加backbuttonItem,但我只想在一個情況下添加它:/ –

    0

    試試這個:

    let viewController = storyboard?.instantiateViewController(withIdentifier: "Login") as! LoginView 
    let customNavigation = UINavigationController(rootViewController: viewController) 
    customNavigation.navigationBar.tintColor = UIColor.black 
    self.present(customNavigation, animated: true, completion: nil) 
    

    override func viewDidLoad() { 
        super.viewDidLoad() 
        self.title = "Login" 
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(self.backButton)) 
    
    } 
    func backButton() { 
        self.dismiss(animated: true, completion: nil) 
    } 
    

    嘗試設置形象的UIBarButtonItem