cocoa Ghost uiview - 初始化后无法访问我的ivar
我有一个奇怪的问题: 当从 UIView 子类创建视图到另一个 UIView 子类的实例中时,我只能在创建它的方法中访问该对象。 在其他方法中,该对象为 NULL :我无法访问或修改或从超级视图中删除该对象。 代码(简化):
// the first class (named Table):
@class Image;
@interface Table : UIView {
Image *image;
}
@end
#import "Image.h"
@implementation Table
- init{
if((self = [super init])) {
self.frame = CGRectMake(0, 416, 320, 416);
self.tag = 416;
// (blah...blah...)
}
return self;
}
@end
// the second class (named Image):
@class Table
@interface Image : UIView{
Table *vueGbif;
}
-(void) createGbif;
-(void) removeGbif;
@end
#import "Table.h"
@implementation Image
-(void) createGbif{
vueGbif = [[Table alloc] init];
[self addSubview:vueGbif];
vueGbif.frame = CGRectMake(0, 0, 320, 416); //<-- here, "vueGbif"
// is not null and can be set
}
-(void) removeGbif{
[vueGbif removeFromSuperview]; //<-- here, vueGbif
// no longer exists : NSLog returns "null".
}
@end
编辑: 问题就在这个时候,很奇怪…… 事实上,实际上,只有当我从第二个类中调用方法“removeGbif”时(我在这个类中创建第一个类的实例),例如通过按钮上的操作,我才能访问 iVar“vueGbif”。 在这种情况下,指令“removeFromSuperview”被正确执行,并且创建的视图被删除。并且“vueGbif”存在并且不为NULL。 但... 如果我向视图“vueGbif”添加一个按钮,在创建它并将其添加到当前视图(这是第二类的实例)时,然后,如果我触摸此按钮以触发第二类中的“removeGbif” ,此时第二类中“vueGbif”等于NULL,removeFromSuperview根本不起作用。
有什么想法吗?
i have a strange problem :
when creating a view from a UIView subclass, into an instance of an other UIView subclass, i have access to the object only in the method in which i create it.
Into other methods, the object is NULL : i can not access or modify or remove the object from the superview.
the code (simplified):
// the first class (named Table):
@class Image;
@interface Table : UIView {
Image *image;
}
@end
#import "Image.h"
@implementation Table
- init{
if((self = [super init])) {
self.frame = CGRectMake(0, 416, 320, 416);
self.tag = 416;
// (blah...blah...)
}
return self;
}
@end
// the second class (named Image):
@class Table
@interface Image : UIView{
Table *vueGbif;
}
-(void) createGbif;
-(void) removeGbif;
@end
#import "Table.h"
@implementation Image
-(void) createGbif{
vueGbif = [[Table alloc] init];
[self addSubview:vueGbif];
vueGbif.frame = CGRectMake(0, 0, 320, 416); //<-- here, "vueGbif"
// is not null and can be set
}
-(void) removeGbif{
[vueGbif removeFromSuperview]; //<-- here, vueGbif
// no longer exists : NSLog returns "null".
}
@end
Edit :
The problem is at this time, very strange...
In fact, actually i have access to the iVar "vueGbif" only if I invoke the method "removeGbif" from within the second class (i create the instance of the first class in this one), for example with an action on a buttton.
In this case, the instruction "removeFromSuperview" is correctly executed, and the view created is removed. And "vueGbif" exists and is not NULL.
But...
If i add a button to the view "vueGbif", when creating it and adding it to the current view (which is an instance of the second class), and later, if i touch this button to trigger "removeGbif" in the second class, at this time, "vueGbif" is equal to NULL in the second class, and removeFromSuperview doesn't work at all.
Any idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论