Display modal view from anywhere in the code

Is it possible to "pop up" from anywhere in the iOS application.

For example, I want the event to cause the view to move (modal), and the event can occur at any time on any application screen.

In addition, we would like this to be something that can be included in other projects, and would prefer that they do not have to do anything special for these projects (except for the wires in the project that refers to).

+5
source share
3 answers

In this case, you are better off playing with appdelegate, but it depends on which application you are using.

create a method in appdelegate that you can call from any view controller

- (void)myMethod {
    MyController *myController = [[MyController alloc] init];
    [self.window.rootViewController presentModalViewController:myController animated:YES];
}

view.

. 1 modelviewcontroller, , modelview , .

- ,

- (void)myMethod {
     MyController *myController = [[MyController alloc] init];
     if([self.window.rootViewController modalViewController]) {
          [(UINavigationController *)self.window.rootViewController.modalViewController pushViewController:myController animated:YES];
     } else {
          UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myController];
           [self.window.rootViewController presentModalViewController:navController animated:YES];
     }
}
+6

: https://gist.github.com/MartinMoizard/6537467

UIViewController. window.rootViewController.

+2

If you want the modal to reject itself (using a button, maybe?), Just call:

[self dismissViewControllerAnimated:YES completion:NULL];

This will automatically redirect it to the view controller that displayed it and reject the modal view controller.

0
source

All Articles