How to do two UINavigationController operations from the same method?

Often I have to fire the current modal view controller, and then immediately press another view controller (so imagine something is sliding down and then moving to the right). What is a smart way to do this?

If you try to perform these two operations at a time, only the first is executed, the second is ignored.

-(void)dismissThenPush{
[self.navigationController dismissModalViewControllerAnimated:TRUE]; //works
[self.navigationController pushViewController:controller animated:TRUE];    //ignored
}

Over the past 12 months, I set some global flag and checked the source controller for this flag in the viewDidAppear method, and then called the pushViewController method. But I'm tired of this hack, there must be a way to do this all in one go from the modal controller.

+3
source share
3 answers

. :

  • () . ( ) ( ).
  • .
  • , , , .

, , .

:
:

- (IBAction)pushModalVC {
    ModalViewController *modalVC = [[ModalViewController alloc] init];

    modalVC.ownerVC = self;

    [self presentModalViewController:modalVC animated:YES];

    [modalVC release];
}

:

- (IBAction)pushSecondVC {
    // this could be accessed via a property rather than loading
    SecondViewController *secondVC = [[SecondViewController alloc] init];

    [self dismissModalViewControllerAnimated:YES];

    [ownerVC.navigationController pushViewController:secondVC animated:YES];

    [secondVC release];
}
+1

- (. question) , (. 2 ), , , , , .

typedef enum {
    ViewControllerPushControllerNone,
    ViewControllerPushControllerSettings,
    ViewControllerPushControllerWhatever
} ViewControllerPushController;

@interface ViewController : UIViewController {
    ViewControllerPushController _pushController;
}
@property(nonatomic, assign) ViewControllerPushController pushController;

@end




@implementation ViewController

@synthesize pushController = _pushController;

- (void)viewDidAppear:(BOOL)animated {
    switch(self.pushController){
        case ViewControllerPushControllerSettings:
            // Push Settings controller
        break;
        case ViewControllerPushControllerWhatever:
            // Push Whatever controller
        break;
    }
    self.pushController = ViewControllerPushControllerNone;

    [super viewDidAppear:animated];
}

@end

, . , , , , .

EDIT. , enum typedef. switch, - :

- (void)viewDidAppear:(BOOL)animted {
    if(self.pushController != nil){
        NSString *selector = [NSString stringWithFormat:@"push%@", NSStringFromClass(self.pushController)];
        [self performSelector:NSSelectorFromString(selector)];
    }
    self.pushController = nil;

    [super viewDidAppear:animated];
}

- (void)pushSettingsViewController {
    if(_settingsViewController == nil){
        _settingsViewController = [[SettingsViewController alloc] init];
    }

    [self.navigationController pushViewController:_settingsViewController animated:YES];
}
0

The trick doesn't seem to animate the first transition.

[self.navigationController dismissModalViewControllerAnimated:NO];
[self.navigationController pushViewController:controller animated:YES];  

Working.

0
source

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


All Articles