Search code examples
iosobjective-cuinavigationcontrollerpresentviewcontroller

Push navigation from current context


Hi i have two Viewcontroller A and B and i am presenting B Controller from A Controller with Current Context and I pasted my coding below. All i want to push from B controller to C controller, I know that no navigation controller allocated when we present and if i present navigationcontroller with rootviewcontroller i cannot achieve transparent result.

 UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        ChooseAddressVc *sec=[story instantiateViewControllerWithIdentifier:@"ChooseAddressVc"];
        sec.myDelegate = self;
        sec.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        sec.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:sec animated:YES completion:^{

        }];

Pushing from Viewcontroller B to C

UIStoryboard*Story=[UIStoryboard storyboardWithName:@"Main2" bundle:nil];
    AddNewAddressVc*choose=[Story instantiateViewControllerWithIdentifier:@"AddNewAddressVc"];
    [self.navigationController pushViewController:choose animated:YES];

Note:I need to push from B to C controller when i present with current context. FOR BETTER UNDERSTANDING:WE CANNOT ALLOCATE NAVIGATION CONTROLLER AS ROOT VIEWCONTROLLER FOR A WHEN WE NEED TO GET TRANSPARENCY EFFECT


Solution

  • In your case self.navigationController in B view controller is nil. You need to create UINavigationController with B view controller as rootViewController. Present created UINavigationController from A instead presenting B view controller, after this you're able to use self.navigationController in B view controller.

    Your code edited:

        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        ChooseAddressVc *sec=[story instantiateViewControllerWithIdentifier:@"ChooseAddressVc"];
        sec.myDelegate = self;
    
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:sec];
        navController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
        [self presentViewController:navController animated:YES completion:^{
    
        }];