导航控制器:存储控制器与模型

发布于 2024-09-02 12:40:21 字数 1071 浏览 5 评论 0原文

我有一个带有表格视图的导航控制器。在我读过的大多数教程中,它们通常在表视图控制器中本地存储一组视图控制器(或其子类),当选择表单元格时,它们用于将其推送到导航堆栈上。

在我当前的项目中,我有很多从 XML 文件加载的数据。这些数据保存在单例类的中心。如果你愿意的话,这是我的“模型”。

因此,在我的表视图控制器中,我没有视图控制器数组(当视图控制器已经存储在其他地方时,我不想将冗余数据存储在视图控制器中)。相反,我从单例访问数据并创建一个视图控制器,使用适当的数据对其进行初始化,然后每次选择表单元格时将其推送到导航堆栈上。

这是 tableview:didselectricwatindexpath: 方法的示例:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    DetailViewController *nextController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    // Here I setup some data from the singleton (just an example)
    nextController.title = [mySingleton.titles objectAtIndex:[indexPath row]];

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

    [nextController release];
}

所以我的问题是这种方法是否有可能遭受性能损失?我担心每次用户选择表格单元格时分配和释放内存。另一方面,由于我可能会拥有大量数据,因此我不想通过将其存储在控制器数组中来重复这些数据。

我总是可以让我的单例准备视图控制器并存储它们,而不仅仅是原始数据。但我觉得有点奇怪,模型总是隐含在控制器中。是否有一种正确的方法可以独立于控制器知识来存储模型?

你会如何处理这种情况?

提前致谢。

I have a navigation controller with a table view. In most tutorials I've read they usually have an array of view controllers (or subclasses of) stored locally in the table view controller that they use to push onto the navigation stack when a table cell is selected.

In my current project I have a lot of data that is loaded from an XML file. This data is kept central in a singleton class. This is my 'model' if you will.

So in my tableview controller I don't have an array of view controllers (I didn't want to have redundant data stored in my view controller when it's already stored elsewhere). Instead I access the data from the singleton and create a view controller, initialize it with the appropriate data and then push that onto the navigation stack every time a table cell is selected.

This is a sample of the tableview:didselectrowatindexpath: method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    DetailViewController *nextController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    // Here I setup some data from the singleton (just an example)
    nextController.title = [mySingleton.titles objectAtIndex:[indexPath row]];

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

    [nextController release];
}

So my question is does this approach have the potential to suffer performance loss? I'm worried about allocating and releasing memory every time the user selects a table cell. On the other hand, since I will potentially have a lot of data, I don't want to have to repeat this data by storing it in an array of controllers.

I could always just have my singleton prepare the view controllers and store those instead of just the raw data. But I find it a bit weird that the model is always implicit in the controller. Is there a proper way of having the model stored independently of controller knowledge?

How would you approach this situation?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2024-09-09 12:40:22

你的做法是对的。
在这行代码中:

nextController.title = [mySingleton.titles objectAtIndex:[indexPath row]];

您不是“分配和释放”任何数据,而是指针的数据。 nextController.title 将指向 mySingleton.titles 中的某个元素。只要您不对数组调用任何 copy 方法,数据就仅存在于您的单例类中。

Your approach is right.
In this line of code:

nextController.title = [mySingleton.titles objectAtIndex:[indexPath row]];

you are not "allocating and releasing" any data but the pointer's data. nextController.title will point to some element inside mySingleton.titles. The data lives only inside your singleton's class as long as you don't call any copy method on the arrays.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文