开发者

Iphone application, object oriented design ideas

开发者 https://www.devze.com 2023-03-18 05:04 出处:网络
I am a java developer learning Objective C for an IPhoneproject, my question is about the OOP design in Cocoa. I have a view based application which communicates with a web service and recives an xml,

I am a java developer learning Objective C for an IPhone project, my question is about the OOP design in Cocoa. I have a view based application which communicates with a web service and recives an xml, parse it and map its contens an appropriate view component (e.g if thats a date show the question with da开发者_如何学编程tepicker, if question has 2 values show it with a segmented control, if more with a pickerview..etc) so its a dynamic questionary with many pages.

What is the best design possible here considering the cocoa framework, for instance if I create a class called "Connection" for making web request, can I create a connnection object from my ViewController class and use it? or I should use delegete classes for that..because my class will include methods like:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
 [webData setLength: 0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
 [webData appendData:data];
}

Does this kind of implemented methods above must have placed in a viewcontroller class?

And second question, how can I do the design for the mapping the xml response into related UI view components? I want to make the code as object orient as possible and not too many if elses in my view controller class, but instead it should only receive a let's say "uiview object" filled with necessary view components and it will just show it. but the mapping and reasoning should be done somewhere else..where can I do that and then where can I put the methods like for instance;

-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [itemArray count];
} 

does this have to be in a controller class? if not how can I access this methods.

Hope I could made my self clear.


For part 1, it sounds like you want to make a factory/engine class that will handle communication with the services and parsing of the data. In that class header declaration you will want to create a protocol (interface in java terms) that the viewcontrollers will implement for delegate methods. This class will also have a member that is of type id that will hold a delegate object for callbacks.

To answer part 2, you can have your factory/engine class actually create the objects during parsing and return them to the viewcontroller in said delegate calls.


Your explanations are clear, yes. The view controller makes the interface between your model objects and your views. It is responsible for updating the views when the model values change, and it also responds to user events to update the model.

The delegate methods may be in your view controller as long as it is the delegate for your NSURLConnection object, but it may be any object you want that you set as the delegate and of course you must ensure this object responds to the required delegate methods.

In my opinion, there is no better design, i usually set a view controller object as the delegate for a NSURLConnection.

About the second question, i have no advice to give about design. It is a bit hard to imagine such a system and i have no idea which solution would be better to use. You could use an indexed array that will contains strings. The indexes would represent the number of possible answers, while the string is the class name for the object to use.

Good luck.

0

精彩评论

暂无评论...
验证码 换一张
取 消