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:
- Yes. Before you push the detail view controller set it's
delegate
property toself
. - You need to declare that your master view controller implements the DetailViewDelegate protocol. Learn how to do that by reading Apple's Documentation.
- 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
2010-10-08 18:19:42
Can you show some code on how to do that?
awakeFromNib
2010-10-08 22:42:07
What's the purpose of that second line? Is it really necessary? I get an error message saying "Expected ')' before detailViewController'.
awakeFromNib
2010-10-09 00:15:50
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
2010-10-09 01:15:24
`@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
2010-10-09 01:19:06
I changed the name of the `BOOL` variable, too.
kubi
2010-10-09 01:19:57
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
2010-10-09 06:17:07
I updated my answer...
kubi
2010-10-09 15:32:33
When I push the view controller it crashes saying setDelegate:]: unrecognized selector.
awakeFromNib
2010-10-09 16:30:15
Did you `@synthesize` your delegate property?
kubi
2010-10-10 20:09:06