美文网首页
A 控制器 Push 到 B 控制器 presentViewCo

A 控制器 Push 到 B 控制器 presentViewCo

作者: Onlyoner | 来源:发表于2017-01-06 15:32 被阅读0次

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

相关文章

网友评论

      本文标题:A 控制器 Push 到 B 控制器 presentViewCo

      本文链接:https://www.haomeiwen.com/subject/xxfxbttx.html