Dismissing both UINavigation views and Modal views at once programmatically

后端 未结 3 1083
梦毁少年i
梦毁少年i 2020-12-01 17:47

I have tried a few answers on this site, but none of them seem to relate to my problem

I have a MasterDetail app which has a two types of segues that I am using. Whe

相关标签:
3条回答
  • 2020-12-01 17:48

    You could try replacing [self.dismissViewControllerAnimated:YES completion:nil]; with

    self dismissViewControllerAnimated:YES completion:^{
        [parentViewController.navigationController popToRootViewControllerAnimated:YES];
    }
    

    I believe parentViewController will point to the presenting view controller, and the block will cause the parent view controller's navigation controller to pop to the root view controller.

    0 讨论(0)
  • 2020-12-01 17:52

    From the class you presented your modal view

    call dismiss of modal and then perform selector after some delay and then do the

    here is the sample code

    //Write this in the class from where you presented a modal View.
    //Call the function from modal class when you want to dismiss and go to root.
    - (void) dismissAndGoToRoot {
          [self dismissViewControllerAnimated:YES completion:nil];
          [self performSelector:@selector(gotoRoot) withObject:nil afterDelay:0.50];
    }
    
    - (void)gotoRoot {
    
        [self.navigationController popToRootViewControllerAnimated:NO];
    }
    

    Here you will call the function

    //right here is where I would normally dismiss the modal VC //here is where I would like to dismiss all VC

    [self.parentViewController dismissAndGoToRoot];
    

    If this does not work then take an instance variable of ParentViewController in modal class and assign when presenting modal view controller.

    0 讨论(0)
  • 2020-12-01 18:04

    If you want, in iOS 6.0 (and later) projects you can use an unwind segue. For example, you can:

    1. In your top level view controller (the one you want to unwind to, not the controller you're going to unwind from), write an unwind segue method, in this case called unwindToTopLevel (personally, I find it useful if the segue bears some indication as to what the destination of the segue is, for reasons that will become apparent when we get to step 3, below):

      - (IBAction)unwindToTopLevel:(UIStoryboardSegue *)segue
      {
          NSLog(@"%s", __FUNCTION__);
      }
      
    2. In the Interface Builder scene from which you will initiate the unwind, control ⌘-drag from the view controller icon to the exit icon to create the unwind segue:

      enter image description here

      Generally you'd define the segue from a button to the exit outlet (and you're done), but if you want to invoke the segue programmatically, you might want to create it between the controller and the unwind outlet, like shown above.

    3. You'll get a little pop up that includes the name of your unwind segues (from the presenting controllers ... it's like magic):

      enter image description here

    4. If you're going to invoke that segue programmatically, you have to select that unwind segue in the document outline on the left side of the IB window and once you've done that, you can give the unwind segue a storyboard id:

      enter image description here

      I generally use the name of the the unwind segue for the storyboard id, but you can use whatever you want.

    5. Now, having done that, your alert view can invoke the segue:

      - (IBAction)didTouchUpInsideButton:(id)sender
      {
          [[[UIAlertView alloc] initWithTitle:nil message:@"go home" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil] show];
      }
      
      #pragma mark - UIAlertViewDelegate
      
      - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
      {
          if (buttonIndex != [alertView cancelButtonIndex])
          {
              [self performSegueWithIdentifier:@"unwindToTopLevel" sender:self];
          }
      }
      
    0 讨论(0)
提交回复
热议问题