Cocoa Touch UIViewController属性和类设计 [英] Cocoa Touch UIViewController Properties and class design

查看:62
本文介绍了Cocoa Touch UIViewController属性和类设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自定义ViewController。此VC需要加载一些在创建它的事件中已知的数据,并将其推送到将成为其一部分的NavigationController的顶部。

I'm creating a custom ViewController. This VC needs to load some data that is known in the event that creates it and pushes it to the top of the NavigationController that it is going to be part of.

我的问题是,我应该如何将数据从处理自定义ViewController的创建的视图传递到该自定义ViewController。

My question is, how should I pass data from the view that handles the custom ViewController's creation into that custom ViewController.

我已经想到了四个可能的选项,我希望得到反馈,为什么每个是好或不是这个功能。

I've thought of four possible options, and I was hoping to get feedback on why each one is good or not for this functionality.

1)在自定义ViewController中公开公共属性,并根据 - (void)ViewDidLoad中的属性在视图中设置UI元素。

1) Expose public properties in the custom ViewController and set the UI elements in the view based on those properties in - (void) ViewDidLoad.

2)显示实际UI元素本身,并在创建ViewController时设置它们的.text / .image / .whatever属性。

2) Expose the actual UI elements themselves and set their .text/.image/.whatever attributes as the ViewController is being created.

3)为自定义视图创建自定义构造函数,并传递我需要设置UI元素的值

3) Create a custom constructor for the custom view and pass in the values I need to set up the UI elements

4)创建一个自定义模型, ,在创建/推送CustomView之前设置数据,并在ViewDidLoad事件中访问该数据。

4) Create a custom model that both views have access to, set the data before the CustomView is created/pushed, and access that data in the ViewDidLoad event.

我对所有这些都还是新的,确保我理解正确处理这些数据的越区切换。看起来像这样的东西可能是一个简单的答案,但我仍然有点困惑,它可能是真正重要的是这样做,以避免内存丢失/泄漏。

I'm still new to all of this, and I want to make sure that I understand the proper handling of these handoffs of data. It seems like something like this is probably a simple answer, but I'm still a little confused and its probably really important to do this right to avoid memory loss/leaks.

此外,如果任何人关心,我在iTunes U和Mark / Lamarche的开始iPhone开发上使用斯坦福的CS193p类来教我自己的可可豆。我正在一个应用程序与NavigationController和一对夫妇ViewControllers(存在1,如果你熟悉193p)。

Also, in case anyone cares, I'm using Stanford's CS193p class on iTunes U and Mark/Lamarche's "Beginning iPhone Development" to teach myself cocoa for the iPhone. I'm working on an application with a NavigationController and a couple ViewControllers (Presence 1 if you're familiar with 193p).

推荐答案

好吧,我相信有优势,每个方法的缺点,根据你的要求...经常需要一些组合的方法。我相信最常见的,对我来说,无论如何,是做这样的事情,你给它足够的开始。

Well, I believe there are advantages & disadvantages to each of those methods depending on your requirements...often it will require some combination of approaches. I believe the most common, for me anyway, is to do something like this where you give it enough to get started.

MyViewController *vc = [[MyViewController alloc] init]; // (or initWithNibName:bundle:)
// transfer vc values here
vc.value1 = aValue;
vc.value2 = anotherValue;
[self.navigationController pushViewController:vc animated:YES];
[vc release];

视图控制器实例化之后,您有机会将对象传递给它。说MyViewController是一个详细视图,那么你会给它的对象,它将显示的细节。或者,如果它是一个表视图,你可以给它NSArray它将需要显示。然后在viewDidLoad或awakeFromNib或awakeFromCoder,或...你可以填写视图...可以这么说。

After your view controller is instantiated you have an opportunity to pass objects to it. Say MyViewController is a detail view then you'd give it the object it will be displaying the details for. Or, if it's a table view you can give it the NSArray it will need for display. Then in viewDidLoad or awakeFromNib or awakeFromCoder, or... you can fill out the view...so to speak.

这篇关于Cocoa Touch UIViewController属性和类设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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