gpt4 book ai didi

ios - UINavigationController "pushViewController:animated"的完成处理程序?

转载 作者:IT王子 更新时间:2023-10-29 07:29:13 24 4
gpt4 key购买 nike

我打算使用 UINavigationController 创建一个应用程序来呈现下一个 View Controller 。在 iOS5 中,有一种呈现 UIViewControllers 的新方法:

presentViewController:animated:completion:

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

pushViewController:animated:

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

最佳答案

参见 par's answer获取另一个更新的解决方案

UINavigationController 动画与 CoreAnimation 一起运行,因此将代码封装在 CATransaction 中并因此设置一个完成 block 是有意义的。

swift :

对于swift,我建议创建一个这样的扩展

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

关于ios - UINavigationController "pushViewController:animated"的完成处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9906966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com