3

我通过以下方式初始化视图(图像):

Image *myImageView = [[Image alloc]init];
    myImageView.myId = randomImageNumber;
    [myImageView initWithImage:myImage];

在 Image 类中,我执行 Log(LOG1) 并获取先前设置的 randomImageNumber。稍后,在同一个类中,我做了第二个 Log(LOG2)。为什么我的第二个日志没有价值了?

这是我的类图像的实现文件:

@synthesize myId;
-(id) initWithImage: (UIImage *) anImage
{
    NSLog(@"LOG1%d",myId);
    if ((self = [super initWithImage:anImage]))
    {
        self.userInteractionEnabled = YES;
    }
    return self;
}

}
-(void)touchesBegan...
....
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  NSLog(@"LOG2%d",myId);
}

“返回自我”清空了我在头文件中声明并在初始化时设置的 myId。我该如何防止呢?

我的头文件看起来像这样:

@interface Image : UIImageView 
{
   int myId;
}
@property (assign) int myId;

@end
4

2 回答 2

7

我想我找到了:

https://www.google.com/maps/place/Variable/@53.626739,10.025728,17z/data=!3m1!4b1!4m2!3m1!1s0x47b1885360fab615:0x584b82c7dfb5f612

你能检查这个变量是否是你的吗?

好朋友菲尔

于 2014-05-23T16:53:48.943 回答
2

在你的代码中结合一些东西。永远不要在一个对象上多次调用 init,这只会搞砸你的对象。

将其更改为:

Image *myImageView = [[Image alloc] initWithImage:myImage];
myImageView.myId = randomImageNumber;

那是您的问题,默认情况下,在初始化 的子类时NSObject,所有属性都设置为0(或者nil如果它们是指针)。

如果您需要为默认值,myId请执行以下操作:

// Image.m

@implementation

// other code

-(id) initWithImage:(UIImage *) image
{
    if (self = [super initWithImage:image])
    {
         self.myId = randomImageNumber;
    }

    return self;
}

// other code

@end
于 2010-10-15T12:22:05.553 回答