简体   繁体   English

将对象传递给Objective-C中另一个类的最佳方法是什么?

[英]What is the best way to pass an object to another class in Objective-C?

Really enjoying working on my first app after a couple months of non-stop studying. 经过几个月的不间断学习,我真的很喜欢在我的第一个应用程序上工作。 Been given the opportunity to do a charities iOS App. 有机会做了一个慈善的iOS App。 It is very basic app but I'm now stuck and would like some help. 这是一个非常基本的应用程序,但我现在遇到了麻烦,希望获得一些帮助。

I have the following view controllers and one class: 我有以下视图控制器和一个类:

PeopleViewController (datasource and delegate, also holds "people" NSMuteable array)
PersonViewController
Person

I have a PersonViewController and this controller is connected to a view that displays a form with a few text fields and an image upload option. 我有一个PersonViewController并且此控制器连接到一个视图,该视图显示带有一些文本字段和图像上载选项的表单。 It has a submit button at the very bottom that is connected to an action that collects all the data from the fields. 它的最底部有一个提交按钮,该按钮与一个动作收集在一起,该动作从字段中收集所有数据。 Well that's what I intend to do. 那就是我打算做的。

I have a PeopleViewController which is a datasource and delegate . 我有一个PeopleViewController ,它是一个datasourcedelegate It is responsible for providing a list of users to show on the table view. 它负责提供要在表视图上显示的用户列表。 These list of users are taken from an array "people". 这些用户列表取自数组“ people”。 Currently I am populating the array in the viewDidLoad method of the people view controller. 当前,我正在人员视图控制器的viewDidLoad方法中填充数组。 Eventually the datasource will be the clients database but for now this array is acting as that database. 最终, datasource将是客户数据库,但目前此数组充当该数据库。

What I want to do is some how get the data from the PersonViewController which I have stored in a Person class instance over into the PeopleViewController array. 我想做的是一些如何从存储在Person类实例中的PersonViewController获取数据到PeopleViewController数组中。 When the submit button of the PersonViewController form is tapped an instance of Person is created and then the data from the fields is stored in that new object. 点击PersonViewController表单的PersonViewController按钮时,将创建Person的实例,然后将字段中的数据存储在该新对象中。 The thing is that people array is populated during viewDidLoad. 问题是在viewDidLoad期间填充了人员数组。

Questions: 问题:

How can I get the newly created and populated Person object in the PersonViewController into the people array in the PeopleViewController class? 如何将PersonViewController新创建并填充的Person对象放入PeopleViewController类的people数组中?

What would be the best way to achieve this? 实现这一目标的最佳方法是什么?

Am I right in saying after viewDidLoad is run the people array holding existing "people" remains present in the heap and I can just some how add this newly created object to that array? 我是说对了吗?在运行viewDidLoad之后,持有现有“人”的人数组仍然存在于堆中,我可以通过一些方法将新创建的对象添加到该数组中吗?

Slightly confused. 有点困惑。

I have thought of a few ways but some seem quite tedious. 我想到了一些方法,但有些看起来很乏味。 I'd like to see how an experienced developer would do this and understand why. 我想看看经验丰富的开发人员如何做到这一点,并理解原因。

Regards 问候

First of all your array must be an NSMutableArray, otherwise you can't add or remove objects from the array. 首先,您的数组必须是NSMutableArray,否则您将无法从数组中添加或删除对象。

Once you're sure that your array is mutable, you can just call 一旦确定阵列是可变的,就可以调用

[personArray addObject:newPerson];

You can do this, even if you fill your array in viewDidLoad . 即使将数组填充到viewDidLoad ,也可以执行此操作。 Note that adding an item to the array is just one step, you still need to update the user interface. 请注意,将项目添加到阵列只是一个步骤,您仍然需要更新用户界面。

EDIT: 编辑:
To pass the newly created Person object from one controller to another you have several options. 要将新创建的Person对象从一个控制器传递到另一个控制器,您有几种选择。 If PersonViewController has a pointer to PeopleViewcontroller you can just make the array a public property. 如果PersonViewController具有指向PeopleViewcontroller的指针,则只需将数组设为公共属性即可。

[myPeopleViewController.personArray addObject...]

It's really hard to answer this question because I would need to see how both your ViewControllers are being instantiated, and what's their relationship one another. 真的很难回答这个问题,因为我需要查看如何实例化两个ViewController,以及它们之间的关系是什么。

Maybe a Singleton is interesting for you. 也许Singleton对您来说很有趣。 Especially if you will enhance your app to share data with other ViewControllers. 特别是如果您将增强您的应用程序以与其他ViewController共享数据。

Here a good site to learn something about DesignPattern and Singleton http://www.raywenderlich.com/46988/ios-design-patterns 这是一个学习DesignPattern和Singleton的好网站,网址为http://www.raywenderlich.com/46988/ios-design-patterns

Hope thats helps you a little bit and have fun with iOS. 希望这对您有所帮助,并能在iOS上玩得开心。

Greetz 格尔茨

What I had done originally was have an action attached to my forms submit button. 我最初所做的是在表单提交按钮上附加了一个操作。 Then I declared an instance variable and initialised it inside the action. 然后,我声明了一个实例变量,并在操作中对其进行了初始化。 Then was under the impression I could grab the instance in my prepareforsegue method and access it's properties I had set in the action but I kept getting null when trying to read a properties from the object instance. 当时的印象是我可以在我的prepareforsegue方法中获取实例并访问它在操作中设置的属性,但是当尝试从对象实例读取属性时,我一直得到null。

I deleted the action and removed it's connection then initialised a new object to store the text field values inside the prepareforsegue method and this worked fine for me. 我删除了该动作并删除了它的连接,然后初始化了一个新对象以将文本字段值存储在prepareforsegue方法内,这对我来说很好用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Objective-C 2.0中的类对象是什么? - What is a class object in Objective-C 2.0? 在Objective-C中传递包含pdf名称的字符串的最佳方法是什么? - What is the best way to pass a string containing a pdf name in Objective-C? 将(objective-c)JNI中的jbyteArray传递给Java的最佳方法是什么? - What is the best way to pass a jbyteArray from (objective-c) JNI to Java? 如何在没有对象引用的情况下将Objective-C类实例变量传递给另一个类实例变量? - How to pass Objective-C class instance variable to another class instance variable without object reference? 对可能被objective-c中的另一个线程锁定的对象进行“脏读”的最佳方法 - Best way to do a “dirty read” of an object that may be locked by another thread in objective-c 建议在Objective-C中使用类方法和对象方法的最佳方法? - Suggest best way to use class methods and object methods in Objective-C? 从 Objective-C 移植到 C++ 的最佳方法是什么? - What is the best way to port from Objective-C to C++? 在 objective-c 的不同类之间访问/共享 object 实例的最佳方法是什么? - What is the best way to access/share object instances among different classes in objective-c? 在Objective-C中为类定义私有方法的最佳方法 - Best way to define private methods for a class in Objective-C 哪个是在Objective-C中为类定义私有方法的最佳方法? - Which is the best way to define private methods for a class in Objective-C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM