UINavigationController的完成处理程序

UINavigationController的完成处理程序

本文介绍了UINavigationController的完成处理程序" pushViewController:animated"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是关于使用 UINavigationController 创建应用程序来呈现下一个视图控制器。
iOS5有一种新方法来呈现 UIViewControllers

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

presentViewController:animated:completion:

现在我问我为什么没有完成处理程序的UINavigationController
只有

Now I ask me why isn´t there a completion handler for UINavigationController?There are just

pushViewController:animated:

是否可以创建我自己的完成处理程序,如新的 presentViewController:animated:completion:

Is it possible to create my own completion handler like the new presentViewController:animated:completion: ?

推荐答案

请参阅 par的回答另一个和更新的解决方案

See par's answer for another and more up to date solution

UINavigationController 动画以<$ c $运行c> CoreAnimation ,因此将代码封装在 CATransaction 中是有意义的,从而设置完成块。

UINavigationController animations are run with CoreAnimation, so it would make sense to encapsulate the code within CATransaction and thus set a completion block.

Swift

对于swift,我建议创建一个扩展名

For swift I suggest creating an extension as such

extension UINavigationController {

  public func pushViewController(viewController: UIViewController,
                                 animated: Bool,
                                 completion: @escaping (() -> Void)?) {
    CATransaction.begin()
    CATransaction.setCompletionBlock(completion)
    pushViewController(viewController, animated: animated)
    CATransaction.commit()
  }

}

用法:

navigationController?.pushViewController(vc, animated: true) {
  // Animation done
}

Objective-C

标题:

#import <UIKit/UIKit.h>

@interface UINavigationController (CompletionHandler)

- (void)completionhandler_pushViewController:(UIViewController *)viewController
                                    animated:(BOOL)animated
                                  completion:(void (^)(void))completion;

@end

实施:

#import "UINavigationController+CompletionHandler.h"
#import <QuartzCore/QuartzCore.h>

@implementation UINavigationController (CompletionHandler)

- (void)completionhandler_pushViewController:(UIViewController *)viewController
                                    animated:(BOOL)animated
                                  completion:(void (^)(void))completion
{
    [CATransaction begin];
    [CATransaction setCompletionBlock:completion];
    [self pushViewController:viewController animated:animated];
    [CATransaction commit];
}

@end

这篇关于UINavigationController的完成处理程序&quot; pushViewController:animated&quot;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:19