Completion handler for UINavigationController “pushViewController:animated”?

前端 未结 9 1067
礼貌的吻别
礼貌的吻别 2020-11-30 18:08

I\'m about creating an app using a UINavigationController to present the next view controllers. With iOS5 there´s a new method to presenting UIViewControl

9条回答
  •  萌比男神i
    2020-11-30 18:18

    Swift 2.0

    extension UINavigationController : UINavigationControllerDelegate {
        private struct AssociatedKeys {
            static var currentCompletioObjectHandle = "currentCompletioObjectHandle"
        }
        typealias Completion = @convention(block) (UIViewController)->()
        var completionBlock:Completion?{
            get{
                let chBlock = unsafeBitCast(objc_getAssociatedObject(self, &AssociatedKeys.currentCompletioObjectHandle), Completion.self)
                return chBlock as Completion
            }set{
                if let newValue = newValue {
                    let newValueObj : AnyObject = unsafeBitCast(newValue, AnyObject.self)
                    objc_setAssociatedObject(self, &AssociatedKeys.currentCompletioObjectHandle, newValueObj, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                }
            }
        }
        func popToViewController(animated: Bool,comp:Completion){
            if (self.delegate == nil){
                self.delegate = self
            }
            completionBlock = comp
            self.popViewControllerAnimated(true)
        }
        func pushViewController(viewController: UIViewController, comp:Completion) {
            if (self.delegate == nil){
                self.delegate = self
            }
            completionBlock = comp
            self.pushViewController(viewController, animated: true)
        }
    
        public func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool){
            if let comp = completionBlock{
                comp(viewController)
                completionBlock = nil
                self.delegate = nil
            }
        }
    }
    

提交回复
热议问题