How to dismiss own view controller and present another view controller in a button tap?

前端 未结 4 822
耶瑟儿~
耶瑟儿~ 2020-12-15 12:47

Let\'s say I have 3 view controller labeled \"A\",\"B\" and \"C\". Right now, \"A\" is the rootViewController of the window and it presents \"B\" modally when a button is ta

4条回答
  •  醉酒成梦
    2020-12-15 13:08

    You are taking about a Button lets name it controlButton. Pass that button with B and C with custom init method. That means your UIViewController A is having controllButton reference. Using the method

    - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents 
    

    set the trigger block in A and like this

    [_controllButton addTarget:self action:@selector(controllButtonTapped:)....];
    
    - (void)controllButtonTapped:(id)sender {
    
        [self dismissViewControllerAnimated:YES completion:^{
    
            // present you c here
    
            [self presentViewController:c animated:YES completion:NULL];
        }];
    }
    

    But the best option is to go with “Mediator Design pattern” where a coordinator is coordinating your present and dismiss actions.

提交回复
热议问题