我的变量在哪里?目标-c

发布于 2024-09-27 10:59:08 字数 857 浏览 5 评论 0原文

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

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

在 Image 类中,我执行 Log(LOG1) 并获取之前设置的 randomImageNumber。 后来,在同一个班级中,我做了第二个日志(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);
}

“return self”清空了我在头文件中声明并在初始化时设置的 myId。 我该如何防止这种情况发生?

我的头文件如下所示:

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

@end

I initialize a view(Image) through:

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

At the Image class I do a Log(LOG1) and get the previously set randomImageNumber.
Later on, in the very same Class, I do a second Log(LOG2).
Why does my second log have no value anymore ?

Here my implementation-file of the Class Image:

@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);
}

The "return self" empties myId which i declared in the header-file and which was set at the initialisation.
How do I prevent that ?

my Headerfile looks like this:

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

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2024-10-04 10:59:08

在你的代码中添加一些东西。永远不要在一个对象上多次调用 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

Couple things in your code. NEVER call init more than once on an object, that just screws up your object.

Change it to this:

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

That is your problem, by default when initializing a subclass of NSObject, all properties are set to 0 (or nil if they are pointers).

If you need to have a default value for myId then do this:

// Image.m

@implementation

// other code

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

    return self;
}

// other code

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文