2016-03-30 75 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

哦,是的,编辑后很好! –

相关问题