How to call a method when presenting a view controller from a modal controller

I have a modal view controller that I called from another controller. After dismissing the modal view controller, I want the method that represented this modal view to be specified on the view manager. What is the easiest way to do this?

I tried to do this in my modular view controller: [(ParentViewController*)self.presentingViewController foo]; before calling [self dismissViewControllerAnimated:YES completion:nil]; .

Xcode gives me an error saying that foo is not recognized, although it is defined and prototyped in the controller. If your solution includes blocks, I really don't understand them, so I would appreciate it if you would add more details. Thanks.

ParentViewController.h

 @interface ParentViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> { NewAssignmentViewController *newAssignmentViewController; TableViewDataSource *data; } -(void)foo; @end 
+4
source share
3 answers

You need to get the correct link to your view controller as follows:

 ParentViewController *presenter = [(UITabBarController *)self.presentingViewController viewControllers][0]; // 0 is assuming that ParentViewController is in the first tab. Change if necessary [presenter foo]; 

Another way to do this is to use delegation, but this is the answer at another time.

+11
source

If you use a Storyboard segue to present your view controller, you can reject it using the Unwind Segue. Segue unwinding is a special kind of segue that unwinds presented view controllers in front of the host.

To do this, you must create a method in the view of the view controller with the following signature:

 - (IBAction)unwindAction:(UIStoryboardSegue*)unwindSegue; 

This is different from the standard IBAction , because the parameter type is UIStoryboardSegue* instead of the usual id type (you don't need to call it unwindSegue: it can be modalViewFinished: or something else you like - the important part is that it has an IBAction return type and parameter type UIStoryboardSegue* ).

Once you have defined this method, in your storyboard you control the drag from the modal view controller icon (under its view, in a small line of icons) and release the connection on the green exit sign. This will create a spread, which you must give an identifier in the attribute inspector. Unwind segues will not be displayed visually in the storyboard canvas, so you will have to find it in the list of items on the left side of the canvas (by default it crashed, expand it by clicking the small circular button in the lower left corner of the canvas’s manual corner).

Once you do this, instead of calling [self dismissViewControllerAnimated:YES completion:nil] just call [self performSegue:<Identifier you gave the unwind segue>] . During this process, the unwindAction: method defined on the view presentation controller and the prepareForSegue: method on the modal view controller must be called. You can do everything you need to do in these methods (for example, calling the foo method from unwindSegue: .

+2
source

You call the method on the UIViewController, which is your MainView, and pass it your UIViewController, which you want to be an ActionSheet.

 UIActionSheet *actionSheetController =[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"show otherview", nil]; [self presentModalViewController:actionSheetController animated:YES ]; 

To reject a UIActionSheet, dimissWithClickedButtonIndex: animated: this is a method for a UIActionSheet that you can implement. The method can be called by anyone (therefore, if you want to reject it from your main view, refer to the action sheet and do something like

 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { { switch (buttonIndex){ case 0: { [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; } break; case 1: { MyClass *myclassObject = [[MyClass alloc]init]; [myclassObject foo]; } } } 

The method is also called whenever the user "cancels" the user clicks.

Use this ModalViewControllers for a better understanding ...!

0
source

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


All Articles