Question

J'ai besoin d'informations sur la manière d'assigner, de conserver des objets.

Par exemple - Si nous avons deux points de vueContrôleurs et que nous devions transmettre une matrice de données de ViewContrlR 1 à ViewContrl 2, comment pouvons-nous envoyer l'objet à partir de la vue 1 pour la voir 2 et le relâcher dans la vue 1 et le retenir en vue 2.

Un opérateur simple= attribue simplement l'adresse qui pointe à nouveau pour voir 1 objet.Quelle est la meilleure façon pour que nous puissions libérer obj en vue 1 et conserver un nouvel objet dans la vue 2 lorsqu'il est passé de la vue 1.

Était-ce utile?

La solution

Create a NSMutableArray in your view controller 2 and declare a retain property for it.

@interface VC2 : UIViewController
{
   NSMutableArray *mutableArrayInVC2
} 
@property (nonatomic, retain) NSMutableArray *mutableArrayInVC2

Then in your view controller one you can pass it with:

viewController2Instance.mutableArrayInVC2 = mutableArrayInVC1

And it's safe to release it with:

[mutableArrayInVC1 release];

[EDIT TO ADDRESS YOUR COMMENT]

When you declare a retain property for your mutableArrayInVC2 and pass mutableArrayInVC1 to it, "behind the scenes" you are accessing the variable via its setter method as per below:

-(void)setMutableArrayInVC2:(NSMutableArray *)arrayValue
{
    [arrayValue retain]; // This is your mutableArrayInVC1
    [mutableArrayInVC2 release]; // This is nil the first time you access it which is cool - we can send messages to nil in ObjC
    mutableArrayInVC2 = arrayValue; // So basically you end up doing and assignment but only after retaining the object so it is pointing to the same memory address BUT it is now 'owned' by your VC2 instance.
}

Hope it makes sense! Rog

Autres conseils

Also, you may want to check out this article for info on retain count mechanisms in general. It's very similar to what's in Cocoa Programming for Mac OS X which IMO is one of the best intro books on Cocoa and Obj-C in general. I'm not sure how much experience you have with Obj-C/Cocoa but if you're looking for that kind of intro it's a great place to start.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top