So I've run into this a few times, and am new to OOD so not sure if there is a good way to do this or not.
But basically, I have a MainViewController, and then I push a new DetailViewController. In my MainViewController, I have a Reset method that basically resets everything to their default values.
If I want to put the button to call Reset in the DetailViewController though, how do I call the method since it's in the MainViewController class?
What I've done before is have a reference to the ParentController (in this case,开发者_开发百科 MainViewController), and then call it that way from the DetailViewController. I don't know if this is a good practice though and if there are better ways to do something like this.
Thanks.
You probably want to have the MainViewController be a delegate to DetailViewController. Apple's frameworks use this pattern all over the place so you can use that as an example. When you do references like this, be sure it doesn't form a retain cycle or you will probably leak memory; don't retain the delegate.
You are following the "delegate pattern" and its one of the very good designs. I would recommend using the same approach. You can check out the book "Cocoa Design Patterns", it should provide you a good understanding of design patterns.
In your case, Create a delegate of type "id" in DetailsViewController, write properties for it and set it when you are pushing the DetailsViewController object from your main view. Then call the method using "ResondsToSelector" on that delegate. Let me know if you want a code snippet.
First of all, consider if it makes sense to have a reset button in the detail view. It might not make sense from a UI organization standpoint for the same reason it's a little inconvenient from an OOD perspective.
Okay, suppose you have decided that it you do want to have the button. Then in some sense it seems that DetailViewController has to be aware of the larger context it's in. It has to know that there's something to reset. I think it's fine to assign a property on DetailViewController a pointer to MainViewController, and declare it as MainViewController *. Unless it's somehow useful for DetailViewController to know that it needs to reset something, but not really know what, then I don't think you need to abstract the fact that DetailViewController is talking to a MainViewController, and leave open the possibility that DetailViewController could be the child of a different kind of parent. So you don't need the protocol.
Option C, you can have some object representing your data model. I will say it is an instance of class Thing, and conforms to protocol ThingModel. You give both MainViewController and DetailViewController a pointer to a ThingModel, i.e. a property declared as id. When they want to reset, they call [self.thingModel reset].
Now, that would reset your data, but how do you reset your views? Many possible options. For one, when DetailViewController calls reset, the next lines can have DetailViewController do what it needs to do to reflect the data model, which is now empty. And maybe MainViewController doesn't do anything yet, but updates itself to match the data model next time it's shown.
精彩评论