简体   繁体   中英

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. 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. 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 . 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". Currently I am populating the array in the viewDidLoad method of the people view controller. Eventually the datasource will be the clients database but for now this array is acting as that database.

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. 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. The thing is that people array is populated during viewDidLoad.

Questions:

How can I get the newly created and populated Person object in the PersonViewController into the people array in the PeopleViewController class?

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?

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.

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 . 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. If PersonViewController has a pointer to PeopleViewcontroller you can just make the array a public property.

[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.

Maybe a Singleton is interesting for you. Especially if you will enhance your app to share data with other ViewControllers.

Here a good site to learn something about DesignPattern and Singleton http://www.raywenderlich.com/46988/ios-design-patterns

Hope thats helps you a little bit and have fun with 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM