2017-02-27 41 views
0

我有一個項目,其中包含一個選項卡控制器,作爲主'頁面'。關閉UITabController裏面的視圖控制器

我想要添加一個UITabBar按鈕項目,它提供了一個視圖控制器modaly,並在該視圖控制器中,添加一個關閉按鈕,關閉視圖控制器並返回到前一個選項卡選擇。

爲了澄清,這是iOS的中型應用程序使用的東西,當您單擊創建發佈項目時,它將以模態形式呈現並在您想要時將其解散。

我可以呈現視圖控制器,但我不能解僱它。

希望我可以理解。

例子: Pressing the middle button

+0

我讀過在這裏,我可以添加一個空的標籤作爲佔位符,然後添加一個按鈕,因爲它的子視圖,然後在按鈕屬性我可以調用viewcontroller模態。但我怎樣才能添加一個空的標籤作爲佔位符? –

回答

0

所以,我已經成功地解決了這個,因爲我想,希望答案將幫助別人的未來。

首先,我我的第一個觀點裝控制器上設置,可以說,我的「家」的視圖控制器的tabBarController代表是我AppDelegate.swift文件(這是個人喜好),如:

self.tabBarController?.delegate = UIApplication.shared.delegate as? UITabBarControllerDelegate 

然後在我AppDelegate.swift文件我加入類屬性委託UITabBarControllerDelegate這樣我就可以訪問tabBarController委託功能,如

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {}

然後編輯功能與我的特定視圖控制器進行交互。

功能的最終版本:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { 
    if viewController is ViewControllerToPresentModaly { 
     if let newVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "modalVC") { 
      tabBarController.present(newVC, animated: true) 
      return false 
     } 
    } 

    return true 
} 

在這裏,如果你想常規選項卡的行爲,return true或您自己的return false

;)

相關問題