UINavigationController and viewWillDisappear

So, I have a UINavController in my application, and I'm trying to execute a method when the user clicks the back button. I searched everywhere and can only find fragments that really don't make sense out of context.

Is there a way to implement some validation that catches when the user clicks the back button to close the current view? (the viewWillDisappear method for the dropdown has never been called for any reason. I read that this does not happen if you do not redirect this call?) Is this correct, and does anyone have any ideas or suggestions? Thanks in advance.

+3
source share
5 answers

AFAIK, if you add a UINavigationController to a UIView through code, it will not send these messages to it as subviews by default. This will only be done if the UINavigationController itself has received these calls. Perhaps this is your problem (I do not know your view setting).

So, adding the UINavigationController view, be sure to send it these messages manually.

UINavigationController *navigationController = [UINavigationController alloc] initWithRootViewController:rootViewController];

[navigationController viewWillAppear:NO];
[aView addSubview:navigationController.view];
[navigationController viewDidAppear:NO];

At least this is what I found during development. I searched for this for a long time, and I still do not understand the reasons for this.

+2
source

UINavigationControllerDelegate. , , UIViewController . , - , -, , . , , viewDidDisappear viewWillDisappear.

# pragma mark - UINavigationControllerDelegate Methods

- (void)navigationController:(UINavigationController *)navigationController 
  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{
    static UIViewController *vcPointer = nil;

    // If the previous view controller is still around then let send a viewWillDisappear message
    if (vcPointer != nil) {
        if ([vcPointer respondsToSelector:@selector(viewWillDisappear:)]) {
            [vcPointer viewWillDisappear:animated];
        }
    }

    // Keep track of a pointer to the current viewController
    vcPointer = viewController;

    [viewController viewWillAppear:animated];   
}

, , , ( ).

+6

, .

, , .

0

:

  • .

: "". .

, . viewDidLoad:

// LeftButton in Navigation Bar
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPushed:)];
self.navigationItem.leftBarButtonItem = leftBarButton;
[leftBarButton release]; 

.m:

- (void) backButtonPushed: (id)sender  {
// do what you want to do       
}

.h

- (void) backButtonPushed: (id)sender;
0

ViewWillDisappear viewDidDisappear , . , UINavigationController. , -?

-1

Source: https://habr.com/ru/post/1772539/


All Articles