如何在iOS应用程序中处理模型类 [英] How to deal with model classes in iOS application

查看:95
本文介绍了如何在iOS应用程序中处理模型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS应用程序开发的新手,但我正在努力学习如何以最佳方式处理Cocoa。

I am a newbie in iOS application development, but I am trying to learn how to deal with Cocoa in the best way.

我试图了解如何理解保持并正确引用模型对象。

I got stuck trying to understand how to keep and reference the model objects properly.


  1. 许多人说写一个app delegate属性来保存模型然后通过方便引用它app委托singleton的方法。

  2. 其他人说在视图控制器中注入只需要它所需的模型部分(或其子视图需要),但我不明白去做这个。通过财产?通过initWithModel:方法(在这种情况下,我怎么能告诉IB使用该方法?)

  3. 其他人再次说模型应该是单身

  4. 再次,其他人说要使用全局变量(!)

  1. many say to write an app delegate property to hold the model and then reference it through the convenience methods for the app delegate singleton.
  2. others say to "inject" in the view controller only the part of model which it needs (or its subviews needs), but I don't understand how to do this. Via a property? Via an initWithModel: method (and in this case, how can I say to IB to use that method?)
  3. others again say that the model should be a singleton
  4. and again, others say to use global variables (!)

你能不能给我一些提示(和代码)样)?我想以适当的方式学习这些东西,考虑到我将很快转向核心数据。

Could you please give me some hint (and code samples)? I would like to learn the things in the proper manner, considering that soon I will be moving towards Core Data.

推荐答案

摘要:我仔细阅读了主题放置核心数据堆栈的位置。在Brad Larson建议的Cocoa / Cocoa Touch应用程序中,我写了一个关于如何处理模型和不同视图控制器的可能解决方案。该解决方案不使用Core Data,但我相信相同的设计可能会应用于Core Data应用程序。

Abstract: I read carefully the topic Where to place the "Core Data Stack" in a Cocoa/Cocoa Touch application suggested by Brad Larson and I wrote a possible solution on how to deal with a model and different view controllers. The solution doesn't use Core Data, but I believe that the same design may be applied to Core Data apps.

场景:让我们考虑一下一个简单的应用程序,存储有关产品的信息,如名称,描述和价格/单位。启动后,应用程序会显示一个产品列表(带有UITableView);当用户点击产品名称时,应用程序会在另一个视图中显示产品详细信息,并使用产品名称更新导航栏。

Scenario: let's consider a simple application which stores information about products, such as name, description and price/unit. Once launched, the application shows a list of products (with a UITableView); when the user taps on a product name, the application presents product details in another view, updating the navigation bar with the product name.

架构这里的模型非常简单:一个Product对象数组,每个对象都有一个名称,一个描述和一个price属性。

Architecture The model is pretty simple here: an array of Product objects, each one with a name, a description and a price property.

该应用程序有三个主要视图,主要是由Xcode的导航模板创建:UINavigationView(由UINavigationController管理,在app委托中实例化),默认的UITableView(由RootViewController管理,是UINavigationController显示的第一个视图)和DetailView(由DetailViewController类管理)我们必须写。)

The application has got three main views, mostly created by the Navigation template of Xcode: a UINavigationView (managed by the UINavigationController, instantiated in the app delegate), the default UITableView (managed by RootViewController and which is the first view shown by the UINavigationController) and a DetailView (managed by the DetailViewController class we have to write).

让我们从模型的角度看看大计划是什么:

Let's see what's the big plan from the model point of view:


  1. 模型由Application委托实例化/加载为Product对象的NSMutableArray;

  2. 现在将指向模型的指针传递给第一个vi我们的层次结构的控制器,UITableViewController,通过属性。实际上,有人可能会争辩说层次结构中的第一个控制器是UINavigationController,所以我们应该将引用传递给它并从它传递给UITableViewController但是...... Apple说UINavigationController不应该是子类,所以我们不能添加任何属性/方法。实际上它是有道理的,因为UINavigationController的责任始终只是可视化管理,而不是模型操作。

  3. 当用户选择UITableCell时,UITableViewController创建一个新的DetailViewController(带有相关的DetailView),将单个选定的产品作为属性传递,并将详细信息视图推送到UINavigation堆栈的顶部。

这里有一些代码片段:

创建模型:

// SimpleModelAppDelegate.m    

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // products is a protected ivar
    products = [[NSMutableArray alloc] init];

    Product *p1 = [[Product alloc] initWithName:@"Gold" andDescription:@"Expensive metal" andUnitPrice:100];
    Product *p2 = [[Product alloc] initWithName:@"Wood" andDescription:@"Inexpensive building material" andUnitPrice:10];

    [products addObject:p1];
    [products addObject:p2];

    [p1 release];
    [p2 release];

    // Passing the model reference to the first shown controller 
    RootViewController *a = (RootViewController*)[self.navigationController.viewControllers objectAtIndex:0];
    a.products = products;

    // Add the navigation controller's view to the window and display
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)dealloc
{
    // The app delegate is the owner of the model so it has to release it.
    [products release];
    [_window release];
    [_navigationController release];

    [super dealloc];
}

RootViewController可以接收模型引用,因为它具有NSMutableArray属性:

The RootViewController can receive the model reference, since it has a NSMutableArray property:

// RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController

@property (nonatomic, retain) NSMutableArray *products;

@end

当用户点击产品名称时,RootViewController实例化一个新的DetailViewController并再次使用属性将对单个产品的引用传递给它。

When the user taps on a product name, the RootViewController instantiates a new DetailViewController and passes the reference to the single product to it using a property again.

// RootViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    // Passing the model reference...
    detailViewController.product = [products objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:detailViewController animated:YES];

    [detailViewController release];
}

最后,DetailViewController显示模型信息设置其出口viewDidLoad方法。

And, at the end, the DetailViewController shows the model information setting its outlets in the viewDidLoad method.

// DetailViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = product.name;
    self.descriptionLabel.text = product.description;
    self.priceLabel.text = [NSString stringWithFormat:@"%.2f eur", product.unitPrice];
}

您可以在此处下载完整项目: http://dl.dropbox.com/u/1232650/linked/stackoverflow/SimpleModel.zip

You can download the full project here: http://dl.dropbox.com/u/1232650/linked/stackoverflow/SimpleModel.zip

我非常感谢对我的解决方案的任何评论,我渴望学习;)

I will really appreciate any comment to my solution, I am eager to learn ;)

这篇关于如何在iOS应用程序中处理模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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