2016-03-30 30 views
1

我想提醒用戶保存當前視圖控制器的變化如何在離開TabBarController中的當前ViewController之前顯示警報?

比方說,我有這樣的事情:

enter image description here

這裏,裏面TabBarController和導航控制器裏面我有一個「收藏夾」選項卡。我想要顯示警報,如果用戶切換到「聯繫人」

問題是警報顯示在目標ViewController(聯繫人)上方,因此它對用戶來說看起來很奇怪。

測試的解決方案:

第一,我試圖用

override func viewWillDisappear(animated: Bool) { 
    self.leavingAlert() 
} 
//inside FavoritesViewController 

接下來,我想:

class FavoritesViewController: UIViewController, UITabBarControllerDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.tabBarController?.delegate = self 
} 

func leavingAlert(){ 
    let alert = UIAlertController(title: "Alert", message: "You forgot to do something here", preferredStyle: UIAlertControllerStyle.Alert) 
    let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil) 
    alert.addAction(alertAction) 
    self.presentViewController(alert, animated: true, completion: nil) 
} 

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) { 
    self.leavingAlert() 
} 
} 

效果相同

然後,我試圖去夠事件通過TabBarViewController:

class TabBarViewController: UITabBarController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

} 

    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) { 
     if let navigationController = selectedViewController as? UINavigationController { 
      if let activeController = navigationController.visibleViewController as? FavoritesViewController { 
       activeController.leavingAlert() 
      } 
     } 
    } 
} 

還有一次 - 同樣的效果。

請注意,我不打算中斷此UITabBarController塞格。這個想法只是問「保存還是不保存?」,如果「保存」,然後做一些東西,並繼續切換標籤,如果「不保存」 - 切換標籤立即。

謝謝你的幫助。如果在Obj-C中有一個解決方案,請也回答,我會試着去理解這個想法。

+0

從ViewWillDisappear方法調用警報。 – iMuzahid

+0

是的,你可以在我的問題中看到這個嘗試)) –

回答

0

解決方案之一是用普通的IBAction代替segue。您的按鈕聯繫人必須在顯示警報的「touch up inside」事件中調用IBAction,然後在調用您的控制器的performSegueWithIdentifier方法的警報完成處理程序中調用IBAction。

2

您可以創建delegate爲的UITabBarController和重載方法:

optional func tabBarController(_ tabBarController: UITabBarController, 
    shouldSelectViewController viewController: UIViewController) -> Bool 

如果用戶嘗試切換到ViewController(從FavoritesViewController)你從這個方法返回和示警戒。
在警報的回調之一中,您可以切換到目標programmatically

unowned(unsafe) var selectedViewController: UIViewController? 
1

你應該繼承你的UITabBarController並讓它成爲它自己的代表。像這樣的應該工作:

class TabBarController: UITabBarController { 
    var viewControllerToSelect: UIViewController? 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     delegate = self 
    } 

    func showLeavingAlert() { 
     let leavingAlert = UIAlertController(title: "Warning", message: "Do you want to save before you leave?", preferredStyle: .Alert) 

     let saveAction = UIAlertAction(title: "Yes", style: .Default) { (action) in 
      let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))) 
      dispatch_after(delayTime, dispatch_get_main_queue()) { 
       // switch viewcontroller after one second delay (faked save action) 
       self.performSwitch() 
      } 
     } 
     leavingAlert.addAction(saveAction) 

     let cancelAction = UIAlertAction(title: "No", style: .Cancel) { (action) in 
      // switch viewcontroller immediately 
      self.performSwitch() 
     } 
     leavingAlert.addAction(cancelAction) 

     presentViewController(leavingAlert, animated: true, completion: nil) 
    } 

    func performSwitch() { 
     if let viewControllerToSelect = viewControllerToSelect { 
      // switch viewcontroller immediately 
      selectedViewController = viewControllerToSelect 
      // reset reference 
      self.viewControllerToSelect = nil 
     } 
    } 
} 

extension TabBarController: UITabBarControllerDelegate { 
    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool { 
     if let navigationController = selectedViewController as? UINavigationController, _ = navigationController.visibleViewController as? FavoritesViewController { 
      // save a reference to the viewcontroller the user wants to switch to 
      viewControllerToSelect = viewController 

      // present the alert 
      showLeavingAlert() 

      // return false so that the tabs do not get switched immediately 
      return false 
     } 

     return true 
    } 
} 
+0

哦,是的,編輯後很好! –

相關問題