views:

42

answers:

1

In my navigation based app, there is a button that if pressed, will change the view to a detailViewController. Here the user can set several options. One of those options is a bool value. When I return from the detailViewController how can I see what this bool value is?

+4  A: 

Create a protocol (DetailViewDelegate?) and create a delegate property in your DetailViewController. When you instantiate your view controller, set the delegate property to self and use that property to send messages back to your master view controller. The only tricky part is that you need to declare the delegate property as "assign" so that you don't create a retain loop between your detail view and master view.


DetailViewController.h:

@class DetailViewController; // Forward Declaration.
@protocol DetailViewDelegate
- (void)detailViewController:(DetailViewController *)controller didChangeBool:(BOOL)theBool;
@end

@interface DetailViewController : UIViewController {
    id <DetailViewDelegate> delegate;
}

@property (assign) id <DetailViewDelegate> delegate;

@end

That's just the interface, but it should get you most of the way there. Set the delegate property of the detail view and implement a detailViewController:didChangeBool: method in your master view and that's about it.


To answer the questions in your comment:

  1. Yes. Before you push the detail view controller set it's delegate property to self.
  2. You need to declare that your master view controller implements the DetailViewDelegate protocol. Learn how to do that by reading Apple's Documentation.
  3. After you declare that your master view controller will implement the protocol, you need to actually implement it. Add a detailViewController:didChangeBool: method to your master view controller.
kubi
Can you show some code on how to do that?
awakeFromNib
What's the purpose of that second line? Is it really necessary? I get an error message saying "Expected ')' before detailViewController'.
awakeFromNib
It's not necessary. It's declaring a formal interface for your delegate object. Unless you have a good reason to remove it, though, you should leave it in as a debugging aid; it ensures that your delegate implements all the methods that you think it's going to implement. I edited the code to make the warning go away.
kubi
`@class DetailViewController` is a forward declaration. It lets the compiler know that `DetailViewController` is a class and it'll be declared at some point in the future.
kubi
I changed the name of the `BOOL` variable, too.
kubi
Before pushing the detailViewController onto the view stack, I do detailViewController.delegate = self. Is that correct? The compiler complains that the class that I'm in doesn't implement the delegate protocol. So am I supposed to put all that stuff you wrote into the class that does this?
awakeFromNib
I updated my answer...
kubi
When I push the view controller it crashes saying setDelegate:]: unrecognized selector.
awakeFromNib
Did you `@synthesize` your delegate property?
kubi