views:

35

answers:

2

Hi, I have an iPhone application where i'm showing a settings page using modal view

[self presentmodalviewcontroller:tempcontroller animated:yes];

When the user finishes the settings he can come back to the previous page.

[self.dismissmodalviewcontroller animated:YES];

Now i want to reload my main page when user comes back from the settings page. I read some where that i should use @protocol and delegate for that to happen.I have gone through some of the tutorials on the web on this topic. But i'm not been able to do that.I have no knowledge on @protocol and delegate as i'm new to the iPhone development.

Please help me with some good tutorials. It would be greate if you can suggest me any link having step by step description of my need.

Looking forward to your help....

Thanks in advance

Joy

A: 

Joy,

Let's say you have a viewController that is presenting another modally - call it "root".

The modal is called "Modal"

"Root" is going to say,

[self presentModalViewController:Modal];

So how does Root know when to dismiss Modal? The best way to do this, is to make a "protocol" for this behavior.

In the header file for Modal, there would be code like this:

@protocol ModalViewDelegate <NSObject>

-(void)modalViewControllerDidFinish:(UIViewController *)viewController;

@end

Then, there should be an instance variable for modal:

id<ModalViewDelegate> delegate;

with a property:

@property (assign) id<ModalViewDelegate> delegate;

This makes it so every time Modal sends a message to it's property 'delegate', we know that it has the method -(void)modalViewControllerDidFinish:

So let's say there's a button inside Modal that you want to close it. The button simply needs to call [delegate modalViewControllerDidFinish:self];

In the header file for root, you declare the class like this:

@class Root : UIViewController <ModalViewDelegate>

And you implement the method modalViewControllerDidFinish like this:

-(void)modalViewControllerDidFinish:(UIViewController *)controller {
    // any action you need to take on controller
    [self dismissModalViewControllerAnimated:YES];
}

Does this make sense?

DexterW
Thanks Greelmo for your detailed answer. I have implemented that but its giving me error-[UIView modalViewControllerDidFinish:]: unrecognized selector sent to instance 0xd38280Dont know why??
Joy
you have to say Modal.delegate = root;make sure you implemented that method on the root. Do some reading. This is the recommended solution by apple.
DexterW
+2  A: 

Another easier option would be to use the NSNotificationCenter. Have a look at this

http://stackoverflow.com/questions/3417946/sending-data-to-previous-view-in-iphone/3418383#3418383

domino
NSNotification center semantically decouples the modal view from the root... that' doesn't make sense because it's a one to one relationship, and one is a clear delegate of the other.
DexterW