how do i change an image on a button in another view [closed]
how do i change the image that is displayed for a button when the button is in another view.
NSNotificationCenter
is a good mechanism for communicating between views without introducing dependencies. KVO may work well too, depending on exactly what you're doing.
Not sure I understand the question entirely but you pass information between view controllers and their view by storing that information in the data model.
Lets say you have two views A and B. Each view will have a view controller Ac and Bc. Let's say you want to set the image of a button in B based on some setting the user makes in A. For example, based on whether a user selects male of female in view A, the back ground of a button in view B will be blue or pink. (...and no I couldn't think of a better example, sue me.)
That information falls under the purview of the data model because the real reason the button in B will change its background is because of some data created by the user's choices. The data model should be in a separate object from any view controller. It can be anything from a simple array to full blown core data graph. It should be designed such that it knows nothing about any interface.
The data model object needs to be parked some place that the controllers can access it. The quick and dirty way is to park the data model object in the app delegate. The best way is to create a singleton for it.
The key concept here is that view A and view B don't have to know about the other's existence. Their respective view controllers, Ac and Bc, don't have to know about each other either. Each view controller simply writes/reads data to the data model independently.
Let's say your app collects data on a person such as age, sex, height, weight etc and then based on data offers some choices to the user. Let's say view A collects the data and view B displays it. The flow will go like this:
- User makes changes to controls in the UI of view A.
- Controller Ac reads those changes, such as the sex of the user, from its view A.
- Controller Ac calls the the data model and writes the information from A to it.
- Controller Ac closes its view and surrenders control to some other controller.
- Any number of things can happen. Including the app quitting and starting (if the data model saves to persistent store.)
- At some point, view B is need. Controller Bc, accesses the data model and reads the data from it. Then it populates/sets the UI in Bc to match that data. Depending on the sex of the user stored in the data model, it sets the image of the button to blue or pink.
The information flow goes A-->Ac--dataModel-->Bc-->B.
This is how all information is passed between interface elements in the Model-View-Controller design pattern that the iPhone uses. The key idea is that the controllers only need to know about the dataModel and nothing else. Every change in the state of the application reflects a change in the data model.
(Dang, I've got to stop drinking so much coffee before writing answers.)
精彩评论