iPhone应用程序,面向对象的设计思路 [英] Iphone application, object oriented design ideas

查看:77
本文介绍了iPhone应用程序,面向对象的设计思路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名Java开发人员,正在为iPhone项目学习Objective C,我的问题是关于Cocoa中的OOP设计。我有一个基于视图的应用程序,该应用程序与Web服务通信并获取xml,解析并映射其内容以包含适当的视图组件(例如,如果那是一个日期,则使用日期选择器显示该问题,如果问题有2值使用分段控件显示它,如果使用pickerview..etc则显示更多),因此它是一个动态查询表,包含许多页面。



考虑可可框架,这里的最佳设计是什么,例如,如果我创建了一个名为 Connection的类来进行Web请求,那么我可以从中创建连接对象吗?我的ViewController类并使用它吗?或我应该为此使用delegete类。因为我的类将包含以下方法:

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

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

上面的这种实现方法是否必须放置在viewcontroller类中? / p>

第二个问题,我该如何设计将xml响应映射到相关的UI视图组件中?我想让代码尽可能地面向对象,而在视图控制器类中不要过多,但是相反,它应该只接收一个 uiview对象,其中填充了必要的视图组件,它只会显示它。但是映射和推理应该在其他地方完成。.我可以在哪里做,然后在哪里放置例如这样的方法;

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

这是否必须在控制器类中?



希望我可以澄清一下。

解决方案

您的解释很清楚,是的。视图控制器在模型对象和视图之间建立接口。当模型值更改时,它负责更新视图,它还响应用户事件以更新模型。



委托方法可能在视图控制器中,如下所示:只要它是您的 NSURLConnection 对象的委托,但它可以是您想要设置为委托的任何对象,当然,您必须确保该对象响应所需的对象



在我看来,没有更好的设计,我通常将视图控制器对象设置为 NSURLConnection



关于第二个问题,我不建议设计。很难想象有这样的系统,我不知道哪种解决方案会更好。
您可以使用将包含字符串的索引数组。索引将代表可能答案的数量,而字符串是要使用的对象的类名。



祝你好运。


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 datepicker, 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.

解决方案

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.

这篇关于iPhone应用程序,面向对象的设计思路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆