Skip to content

Commit

Permalink
QMUINavigationController暴露willShowViewController接口给子类重写
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoon committed Mar 13, 2017
1 parent 3db8c65 commit d456cfb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
15 changes: 15 additions & 0 deletions QMUIKit/UIMainFrame/QMUINavigationController.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,19 @@
* 初始化时调用的方法,会在 initWithNibName:bundle: 和 initWithCoder: 这两个指定的初始化方法中被调用,所以子类如果需要同时支持两个初始化方法,则建议把初始化时要做的事情放到这个方法里。否则仅需重写要支持的那个初始化方法即可。
*/
- (void)didInitialized NS_REQUIRES_SUPER;

@end

@interface QMUINavigationController (UISubclassingHooks)

/**
* 每个界面Controller在即将展示的时候被调用,在`UINavigationController`的方法`navigationController:willShowViewController:animated:`中会自动被调用,同时因为如果把一个界面dismiss后回来此时并不会调用`navigationController:willShowViewController`,所以需要在`viewWillAppear`里面也会调用一次。
*/
- (void)willShowViewController:(nonnull UIViewController *)viewController NS_REQUIRES_SUPER;

/**
* 同上
*/
- (void)didShowViewController:(nonnull UIViewController *)viewController NS_REQUIRES_SUPER;

@end
25 changes: 22 additions & 3 deletions QMUIKit/UIMainFrame/QMUINavigationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ - (void)viewDidLoad {

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 在这里为什么还需要调用一次,是因为如果把一个界面dismiss后回来这里,此时并不会调用navigationController:willShowViewController,但会调用viewWillAppear
[self renderStyleInNavigationController:self currentViewController:self.topViewController];
[self willShowViewController:self.topViewController];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self didShowViewController:self.topViewController];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
Expand Down Expand Up @@ -255,7 +259,7 @@ - (void)handleInteractivePopGestureRecognizer:(UIScreenEdgePanGestureRecognizer
// 注意如果实现了某一个navigationController的delegate方法,必须同时检查并且调用delegateProxy相对应的方法

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[self renderStyleInNavigationController:navigationController currentViewController:viewController];
[self willShowViewController:viewController];
if ([self.delegateProxy respondsToSelector:_cmd]) {
[self.delegateProxy navigationController:navigationController willShowViewController:viewController animated:animated];
}
Expand All @@ -264,6 +268,7 @@ - (void)navigationController:(UINavigationController *)navigationController will
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
self.viewControllerPopping = nil;
self.isViewControllerTransiting = NO;
[self didShowViewController:viewController];
if ([self.delegateProxy respondsToSelector:_cmd]) {
[self.delegateProxy navigationController:navigationController didShowViewController:viewController animated:animated];
}
Expand Down Expand Up @@ -300,3 +305,17 @@ - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
}

@end


@implementation QMUINavigationController (UISubclassingHooks)

- (void)willShowViewController:(UIViewController *)viewController {
// 子类可以重写
[self renderStyleInNavigationController:self currentViewController:viewController];
}

- (void)didShowViewController:(UIViewController *)viewController {
// 子类可以重写
}

@end

0 comments on commit d456cfb

Please sign in to comment.