From fa4dfcbf425b76cffca9d5a94367106e3802317b Mon Sep 17 00:00:00 2001 From: nixzhu Date: Fri, 24 Jul 2015 16:53:35 +0800 Subject: [PATCH] show cancel if skills dirty --- .../Nav/YepNavigationController.swift | 59 +++++++++++++++---- .../RegisterPickSkillsViewController.swift | 28 ++++++++- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/Yep/ViewControllers/Nav/YepNavigationController.swift b/Yep/ViewControllers/Nav/YepNavigationController.swift index 50631cc1..83ee30c0 100644 --- a/Yep/ViewControllers/Nav/YepNavigationController.swift +++ b/Yep/ViewControllers/Nav/YepNavigationController.swift @@ -18,7 +18,6 @@ class YepNavigationController: UINavigationController, UIGestureRecognizerDelega delegate = self } - // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { @@ -77,14 +76,52 @@ class YepNavigationController: UINavigationController, UIGestureRecognizerDelega return true } - /* - // MARK: - Navigation - - // In a storyboard-based application, you will often want to do a little preparation before navigation - override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { - // Get the new view controller using segue.destinationViewController. - // Pass the selected object to the new view controller. - } - */ - } + +// http://stackoverflow.com/questions/20327165/popviewcontroller-strange-behaviour +// 很难work,无法重载 +//// MARK: UINavigationBarDelegate +// +//extension UINavigationController { +// +//// func navigationBar(navigationBar: UINavigationBar, shouldPopItem item: UINavigationItem) -> Bool { +//// return true +//// } +//} +// +//extension YepNavigationController: UINavigationBarDelegate { +// +// //override +// func navigationBar(navigationBar: UINavigationBar, shouldPopItem item: UINavigationItem) -> Bool { +// +// let forcePop: () -> Void = { +// self.dirtyAction = nil +// self.forcePopAction?() +// self.forcePopAction = nil +// } +// +// if let dirtyAction = dirtyAction { +// +// YepAlert.confirmOrCancel(title: NSLocalizedString("Notice", comment: ""), message: NSLocalizedString("Save before pop?", comment: ""), confirmTitle: NSLocalizedString("OK", comment: ""), cancelTitle: NSLocalizedString("Cancel", comment: ""), inViewController: self.topViewController, withConfirmAction: { +// +// dirtyAction() +// +// forcePop() +// +// }, cancelAction: { +// forcePop() +// }) +// +// return false +// } +// +// return true +// //return super.navigationBar(navigationBar, shouldPopItem: item) +// } +// +// func navigationBar(navigationBar: UINavigationBar, didPopItem item: UINavigationItem) { +//// self.forcePopAction?() +//// self.forcePopAction = nil +// self.popViewControllerAnimated(true) +// } +//} diff --git a/Yep/ViewControllers/Register/RegisterPickSkillsViewController.swift b/Yep/ViewControllers/Register/RegisterPickSkillsViewController.swift index 086b8bd2..fed7504b 100644 --- a/Yep/ViewControllers/Register/RegisterPickSkillsViewController.swift +++ b/Yep/ViewControllers/Register/RegisterPickSkillsViewController.swift @@ -11,6 +11,17 @@ import UIKit class RegisterPickSkillsViewController: BaseViewController { var isRegister = true + var isDirty = false { + didSet { + if !isRegister { + let backBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: "cancel") + navigationItem.leftBarButtonItem = backBarButtonItem + } + + navigationItem.rightBarButtonItem?.enabled = true + } + } + var afterChangeSkillsAction: ((masterSkills: [Skill], learningSkills: [Skill]) -> Void)? @IBOutlet weak var skillsCollectionView: UICollectionView! @@ -39,8 +50,9 @@ class RegisterPickSkillsViewController: BaseViewController { override func viewDidLoad() { super.viewDidLoad() - let doneBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "saveSkills:") - navigationItem.rightBarButtonItem = doneBarButton + let doneBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "saveSkills:") + navigationItem.rightBarButtonItem = doneBarButtonItem + navigationItem.rightBarButtonItem?.enabled = false if !isRegister { navigationItem.titleView = NavigationTitleLabel(title: NSLocalizedString("Change Skills", comment: "")) @@ -70,7 +82,15 @@ class RegisterPickSkillsViewController: BaseViewController { } } + func cancel() { + navigationController?.popViewControllerAnimated(true) + } + @IBAction func saveSkills(sender: AnyObject) { + doSaveSkills() + } + + func doSaveSkills() { YepHUD.showActivityIndicator() @@ -236,6 +256,10 @@ class RegisterPickSkillsViewController: BaseViewController { self.updateSkillsCollectionView() + if !self.isDirty { + self.isDirty = success + } + return success } }