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

前端 未结 4 825
耶瑟儿~
耶瑟儿~ 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:05

    you can do using protocol let say for example as bellow:-

    In to your B viewController setting Protocol :

    @class Bviewcontroller;
    
    @protocol BviewControllerDelegate 
    - (void)BviewcontrollerDidTapButton:
    (Bviewcontroller *)controller;
    
    @end
    
    @interface Bviewcontroller : UIViewcontroller
    
    @property (nonatomic, weak) id  delegate;
    - (IBAction)ButtonTap:(id)sender;
    
    @end
    

    in .m class

    - (IBAction)ButtonTap:(id)sender
    {
        [self.delegate BviewcontrollerDidTapButton:self];
    }
    

    Now in to you A_viewController .h class:

    #import "Bviewcontroller.h"
    
    @interface A_viewController : UIViewcontroller
    

    .m class

    - (void)BviewcontrollerDidTapButton:
    (Bviewcontroller *)controller
    {
        [self dismissViewControllerAnimated:YES completion:^{
    
    
          // here you can create a code for presetn C viewcontroller 
    
        }];
    }
    

    IMPORTANT when you preseting Bviewcontroller from A_viewController do not set delegate with object like

    -(void)presentNextViewCon
    {
                    bViewcontroller *gestureViewCon = [[bViewcontroller alloc]init];
            gestureViewCon.delegate = self;
    
    [self presentViewController:gestureViewCon animated:YES completion:nil];
    
    }
    

    UPDATE

    Here it is i create a demo that working like:

    enter image description here

    SAMPLE CODE LINK http://speedy.sh/2acSC/modelDemo.zip

提交回复
热议问题