The relationship between UIViewControllers

I am new to Objective-C, so I apologize for the stupid question.

I open a controller of the "options" view from my main controller. Both are built into the storyboard. Before and after the presentation of the parameter controller, I need to stop and start the timer on my main controller. After closing the parameter controller (the button causes dismissal) I need to send some information back to my main controller or at least tell the main controller that it needs to update some values.

MAIN QUESTION What is the best way to present a presentation controller and execute some presenter methods before and after opening?

WHAT I GOT I found several ways to do this, but they are all cumbersome, and I suppose there must be some plausible way to do this.

  • Ideally, I would like to use the segue that I installed in the storyboard between the two controllers.
  • I managed to call the program controller programmatically by accessing the storyboard and calling the instantiateViewControllerWithIdentifier instance. It worked, but it looks a bit complicated.
  • I was unable to find the delegate method in the UIViewController to handle the reject event
  • When I tried to access the main controller in the parameter controller through presentingViewController and downcasting, I got a binding error by including my .h file twice (not sure which Obj-C standards use #define).

Appreciate your help ...

+4
source share
5 answers

Thanks for answers.

I finished with

  • Call the prepareForSegue method to execute code before going

  • A call to PerformSelector on a ViewController when a presented view controller is released.

I'm sure other suggestions will work too.

0
source

All this can be done quite easily using storyboards and NSNotificationCenter and NSCoding. In the viewDidLoad method of your main controller, put this code:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"Update" object:nil]; 

Then create this method in the same controller:

 (void)receiveNotification:(NSNotification*)notification { //... } 

If you want to update the main controller with the parameter controller:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"Update" object:self]; 

In addition, I would suggest using NSArchiving to save master data. Just found this tutorial, it looks good. http://samsoff.es/posts/archiving-objective-c-objects-with-nscoding

Basically, create an object that can store information, encode it using nscoding, and then decrypt it whenever you need it. This worked great for me.

Hope this helps!

+2
source

MAIN QUESTION What is the best way to present a presentation controller and execute some presenter methods before and after opening?

Just in case, the above answers are a little more than you would like, I suggest that the easiest way to execute the presenter methods before opening is to do this in the lead method preparation method. If you need to send data to the destination view controller, you can access its properties as follows:

 ViewController *destinationVC = [segue destinationViewController]; 

A simple way to execute presenter methods after opening is to:

 ViewControllerSubclass *previousVC = [self presentingViewController]; 

And then use a class or instance to execute your class or instance methods. You can do this in the target view of WillAppear.

Sorry if you already knew all this; it is often difficult to imagine what level of difficulty is needed.

+2
source

For communication between loosely coupled ViewControllers, you can use NSNotificationCenter:

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html

Here you can send a message to all ViewControllers who need to process some changes (for example, change the font size).

This is very easy to implement, and it keeps some ViewControllers less dependent on each other.

+1
source

I came across this with almost every application that I have on the market. The difference is that I never dared to follow the storyboard path.

The way I could always do this is to provide access functions between controllers. You encounter a linker problem by defining a cross-defined controller as a simple type of UIViewController in the header of the parameter view, and then you include the header of the main view controller only in the .m file. Now, when you call the main view controller rutile from the parameter view, you will have to attribute it to the type of your main view controller!

You also need to provide a routine in the parameter view, which will allow you to set a variable that will contain a pointer to your main view controller for yourself.

An example for your view parameters

 @interface optionsViewController : UIViewController{ UIViewController * myReactiveMainViewController; } -(void)setMyReactiveMainViewController:(UIViewController *)controller; 

Not in .m file for View parameters

 #import "myMainViewController.h" -(void)setMyReactiveMainViewController:(UIViewController *)controller{ myReactiveMainViewController = controller; } 

In any other callback to the main view controller, you will need to do this:

 -(void)returnToMain{ [(myMainViewController *)myReactiveMainViewController someCall:variable]; } 

In this example, of course, suppose your myMainViewController implements a method called "someCall" that takes an input parameter.

+1
source

All Articles