iPhone passing objects reference to UIViewcontroller
I have a NSMutableArray in my rootviewcontroller, it is filled with question objects, which each question holds the necessary question text, its options, and chosen value(which is null if question not answered yet)
I also have other uiview xibs , which I push to navigation controller depending on questiontype.
When a question needed to be asked I want to pass the related question variable to the related .xib
But I want to pass the object reference of t开发者_如何学编程he variable so when user edits, enters some value to that question, I dont want to send back any value but be able to set my question variable's value directly from that viewcontroller subclass (pass by referanace)
So what is correct way of declaring the @property and correct way of initialzing my uiviewcontroller?
In the code below does also the value of that variable in the rootviewcontroller array also changes? I guess not because it looks like I pass by value there but not by the reference, how can I change it?
in rootview controller:
MultiSelectionTableViewController *multiSelectionViewController =[[MultiSelectionTableViewController alloc] initWithNibName:@"MultiSelectionTableViewController" bundle:nil];
NSMutableArray *variableitems;
multiSelectionViewController.question=variableItems[0];
and in uiviewsub controller:
Question * question;
@property(nonatomic, retain);
question.value=@"answer";
Also do you advice using NSManagedObjects or any coredata in this arctitecture? these questions are parsed from an xml and will be sended back as XML.
All objects in Objective C are passed by reference, unless you define the property as a copy. So, any changed in the variableItems[0]
in other view controller will affect the object in the calling view controller. If variableItems[0]
is a object pointer.
If you use a list of object, need to store the data, then certainly its wise to use CoreData.
Try with assign
property. That could send the object by reference.
精彩评论