ViewController.h == A 控制器
B.h == B 控制器
C.h == C 控制器
A 控制器
#import "ViewController.h"
#import "B.h"
#import "C.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 60, 60)];
[button setTitle:@"AAA" forState:UIControlStateNormal];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(pushA) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)pushA
{
B* bVC = [[B alloc] init];
[self.navigationController pushViewController:bVC animated:YES];
}
@end
B 控制器
#import "B.h"
#import "C.h"
@interface B ()
@property(nonatomic,assign)BOOL isFromC;
@end
@implementation B
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(50, 400, 60, 60)];
[button setTitle:@"BBB" forState:UIControlStateNormal];
button.backgroundColor = [UIColor greenColor];
[button addTarget:self action:@selector(pushC) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.isFromC) {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}
-(void)pushC
{
C* ss = [[C alloc] init];
NSLog(@"导航 %p",self.navigationController);
[self.navigationController presentViewController:ss animated:YES completion:^{
self.isFromC = YES;
}];
}
@end
C 控制器
#import "C.h"
@interface C ()
@end
@implementation C
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 100)];
[button setTitle:@"CCC" forState:UIControlStateNormal];
button.backgroundColor = [UIColor orangeColor];
[button addTarget:self action:@selector(dissC) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(@"MMMMM %p",self.presentingViewController);
//
// UINavigationController* navi = (UINavigationController*)self.presentedViewController;
// [navi popToRootViewControllerAnimated:YES];
}
-(void)dissC
{
NSLog(@"EEEEE %p",self.presentingViewController);
[self dismissViewControllerAnimated:YES completion:^{
}];
}
@end