/ /. H
@ Property (weak, nonatomic) IBOutlet UIImageView * picImageStage;
/ /. M
NSString * name = [NSString stringWithFormat: @ "allen.png"];
UIImage * image = [UIImage imageNamed: name];
UIImageView * t = [[UIImageView alloc] initWithImage: image];
self.picImageStage = t; / / correct
/ / Declare a variable to retain, in the implementation file to remove temporary variables.
/ /. H
@ Property (retain, nonatomic) IBOutlet UIImageView * picImageStage;
/ /. M
NSString * name = [NSString stringWithFormat: @ "allen.png"];
UIImage * image = [UIImage imageNamed: name];
self.picImageStage = [[UIImageView alloc] initWithImage: image]; / / correct
/ /. H
@ Property (weak, nonatomic) IBOutlet UIImageView * picImageStage;
/ /. M
NSString * name = [NSString stringWithFormat: @ "allen.png"];
UIImage * image = [UIImage imageNamed: name];
self.picImageStage = [[UIImageView alloc] initWithImage: image]; / / error, why
Reply:
In fact, your first wording is also wrong. . .
You either written, are not applied to the inside of your XIB file corresponding controls (assuming the latter XIB file after running in), because alloc is to generate a new instance (instance), a new memory address. . .
If you remove the IBOutlet Both versions are correct. . .
Reply:
/ / The first way to write:
/ /. H
@ Property (weak, nonatomic) IBOutlet UIImageView * picImageStage;
/ /. M
NSString * name = [NSString stringWithFormat: @ "allen.png"];
UIImage * image = [UIImage imageNamed: name];
UIImageView * t = [[UIImageView alloc] initWithImage: image];
self.picImageStage = t; / / correct
/ / The second wording:
/ / Declare a variable to retain, in the implementation file to remove temporary variables.
/ /. H
@ Property ( retain , nonatomic) IBOutlet UIImageView * picImageStage;
/ /. M
NSString * name = [NSString stringWithFormat: @ "allen.png"];
UIImage * image = [UIImage imageNamed: name];
self.picImageStage = [[UIImageView alloc] initWithImage: image]; / / correct
/ / The third wording:
/ /. H
@ Property ( weak , nonatomic) IBOutlet UIImageView * picImageStage;
/ /. M
NSString * name = [NSString stringWithFormat: @ "allen.png"];
UIImage * image = [UIImage imageNamed: name];
self.picImageStage = [[UIImageView alloc] initWithImage: image]; / / error, why
Thank a21064346 reply, I've got three kinds of writing, I mainly do not understand a third way.
Reply:
I do not know what you said errors are runtime errors or compile errors?
But the reason should be so
The first in the wording
UIImageView * t = [[UIImageView alloc] initWithImage: image];
self.picImageStage = t; / / correct
Although self.picImageStage is weak is also on t no retain. But t itself is not released, so you can follow-up operation, a function to die
The third wording
self.picImageStage = [[UIImageView alloc] initWithImage: image]; / / error, why
No variable load [[UIImageView alloc] initWithImage: image], but self.picImageStage nor its Retain So come out and then destroyed to create.
Therefore, the assignment does not make sense.
Reply:
In the third paragraph of the code, you declare @ property is weak. So UIImageView will be released immediately, because if the same object, without a Strong attribute relationship, weak property relationship is zero.
In the first piece of code, you will first UIImageView assigned to a local variable, the local variable is Strong hidden attributes. If the detachment Strong attribute range, unless you re-assign to it a Strong relationships, or attributes will be released immediately. . If you add the UIImageView subview to another view will happen. So it creates a Strong relationships.
Reply:
1 understand the meaning xib. IBOutlet keyword marked xib in control. Since you file a statement in xib, why should reconstruct UIImageView.
2.weak and strong mechanisms should be applied ARC property. If the arc, then retain not be used.
Reply:
First of all, thank you for your reply,
For closewbq of view, the second point is not too understanding, with the words of the ARC, you can not use retain Well, if I talk about it as a member variable, then I think it should not need it, but with the arc, do not need to write their own dealloc.
Prior did not say clearly, it was not running when the access data, this seems like c + + is not the same, suddenly a little thought, maybe this is understood that when a variable is declared as weak, and he does not have the ability to carry the data, he can only is quoted by retain data after. I do not know understand this.
No comments:
Post a Comment