Rejecting both UINavigation and modal views immediately programmatically

I tried several answers on this site, but none of them seem to concern my problem.

I have a MasterDetail application that uses two types of segues that I use. When you click on a button in the detail view, it uses push segue and clicks on it another detailed view. In the new detailed view (the one that was just clicked) there is a button that calls another UIView (form sheet) using a modal segment.

What I'm trying to do is when the user selects a row, the UIAlertView will display the message, and at the same time (should not be at the same ) it rejects the UIViewcontroller (modal) and returns from the Viewcontroller that was clicked. Basically, I need to remove all view controllers, one modal and one push (nav), so that the view returns to the original main screen from which they started.

UIAlertView works for me, and I can disable the modal viewcontroller using [self.dismissViewControllerAnimated:YES completion:nil]; but I don’t know how to reject the next Viewcontroller (which is in the navigation controller). Using this: [self.navigationController popToRootViewControllerAnimated:NO]; does not work.

Here I want to call a function to remove all views:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlWithIDAndChallenge]; NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; UIAlertView *message = [[UIAlertView alloc] initWithTitle@ "Started" message:@"The challenge has begun!" delegate:nil cancelButtonTitle:@"OK!" otherButtonTitles:nil]; [message show] //right here is where I would normally dismiss the modal VC //here is where I would like to dismiss all VC } 
+6
source share
3 answers

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

  • In your top-level view controller (the one you want to disconnect, not the controller you want to relax from), write a segue unwind method, in this case called unwindToTopLevel (personally, I find this useful if there are some instructions on segue regarding of what is segue's destination, for reasons that will become apparent when we go to step 3 below):

     - (IBAction)unwindToTopLevel:(UIStoryboardSegue *)segue { NSLog(@"%s", __FUNCTION__); } 
  • In the script of the interface builder, from which you will begin to unwind, control ⌘ - open the controller icon of the controller to the exit icon to create unwinding:

    enter image description here

    As a rule, you determine the transition from the button to the output output (and you're done), but if you want to programmatically call up a program segment, you can create it between the controller and the unwinding socket, for example, as shown above.

  • You will get a few tooltips that will show your unwinding name (from the controllers presented ... it's like magic):

    enter image description here

  • If you intend to use the segue program code, you need to select this spread in the outline of the document on the left side of the IB window, and as soon as you do this, you can give the segue decoupling ID to the storyboard:

    enter image description here

    I usually use the decoupling name for the storyboard identifier, but you can use whatever you want.

  • Now, having done this, your warning view may trigger 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]; } } 
+14
source

From the class that you presented in your modal form

reject modal mode and then execute selector after some delay and then execute

here is a 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 call the function

// right here where I usually fire the modal VC // here I would like to reject all VC

 [self.parentViewController dismissAndGoToRoot]; 

If this does not work, take the ParentViewController instance variable in the modal class and assign it when presenting the modal view controller.

+1
source

You can try replacing [self.dismissViewControllerAnimated:YES completion:nil]; on the

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

I believe that the parentViewController will point to the view view controller, and the block will make the parent controller's navigation controller appear in the root view controller.

0
source

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


All Articles