connect GUI to LOGiC
I am trying to build my first 开发者_如何学Goiphone app. first i finish with my app logic whitch contain DataLogic class that have all my arrays. and another 2 classes
then i build my appGUI that contain 2 views. SearchViewClass and ResultViewClass now my problem is how to connect to logic to the GUI. the instance of DataLogic is on my AppDelegate class and i need that the two GUI classes will be able to accsess to this instance but i dont know who to accsess to the instances in the AppDelegate class from the GUI classes?
i also didnt see where the app initialize the GUI classes?? so where is the GUI classes instance??
You use the MVC (Model-View-Controller) design pattern. It should be called the Model-Controller-View pattern because the controller mediates between the model and the view.
In your case the DataLogic class is your model. The views are controlled by a paired view controller object which is an instance or a subclass of UIViewController. Depending on the app, there are many different ways to relate the view controllers to each other, their views and the model.
The easiest to understand would be the Navigation Based App in the Xcode Template projects. It has no data model object but it does have a navigation controller and a RootViewController class its paired view defined in the RootViewController.xib. The rootViewController is an instance of the RootViewController subclass of UIViewController (Usually, RootViewController>UITableViewController>UIViewController.)
In your case, you would add a property to the RootViewController to hold a reference to your DataLogic class. In the app delegate's application:didFinishLaunching:
method you would initialize your DataLogic instance and then set the RootViewController's property to the DataLogic instance.
Now the RootViewController instance connects to the DataLogic instance. When the navigation controller pushes the RootViewController instance on to the navigation stack, the RootViewController instance's view (assigned in the nib file) loads. The RootViewController instance then takes data from the DataLogic instance and populates the UI elements in the view. When the users enters data into the UI the controller takes the data from the UI elements and puts them in the DataLogic instance.
The controllers tie the model to different views. The views never directly communicate with the model and vice versa. Many different views can use the same model and display different attributes of the model merely by writing a new controller to connect the two together.
精彩评论