有关子类属性的帮助

发布于 2024-09-16 17:03:14 字数 871 浏览 11 评论 0原文

我有一个名为 Card 的 UIView 子类,我可以在板上移动它并放在称为插槽的热点上。当我放下卡时,我使用 hitTest 来确定我是否将卡放在我的热点之一上。我想获得该热点的财产,但我无法使其正常工作。我唯一的猜测是 hitTest 返回一个 UIView 而我的热点是 UIView 子类。我得到的错误是“请求非结构或联合中的成员‘slotIndex’”

这是我在卡类中使用的 TouchesEnded 方法

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event { 

   UITouch *touch = [touches anyObject];   
   CGPoint location = [touch locationInView:self.superview];

   [self setUserInteractionEnabled:NO];

   UIView *backView = [self.superview hitTest:location withEvent:nil];

   if ([backView isKindOfClass:[CardSlot class]]) {
      self.center = backView.center;

      NSLog(@"Slot Number: %@", backView.slotIndex);

   } else {
      //Move it back to the top corner
      self.center = CGPointMake(50,50);
   }

   [self setUserInteractionEnabled:YES];

}

我的问题是如何测试我是否处于插槽热点并且然后获取该插槽的属性(UIView 子类)?

I have a UIView subclass called Card that I move around on my board and drop on hot spots called slots. When I drop the card I use the hitTest to figure out if I am dropping the card on one of my hotspots. I want to get a property of that hot spot but I am having trouble getting that to work properly. My only guess is the hitTest returns a UIView and my hot spot is a UIView subclass. The error I get is "Request for member 'slotIndex' in something not a structure or union"

Here is the TouchesEnded method I am using from my Card Class

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event { 

   UITouch *touch = [touches anyObject];   
   CGPoint location = [touch locationInView:self.superview];

   [self setUserInteractionEnabled:NO];

   UIView *backView = [self.superview hitTest:location withEvent:nil];

   if ([backView isKindOfClass:[CardSlot class]]) {
      self.center = backView.center;

      NSLog(@"Slot Number: %@", backView.slotIndex);

   } else {
      //Move it back to the top corner
      self.center = CGPointMake(50,50);
   }

   [self setUserInteractionEnabled:YES];

}

My question is how do I go about testing if I am in a slot hot spot and then get the properties of that slot (UIView Subclass)?

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

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

发布评论

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

评论(1

筱武穆 2024-09-23 17:03:14

为了帮助编译器,您需要在确定指针为 CardSlot 后将其强制转换为 CardSlot。这样编译器就可以知道 slotIndex 属性。例如:

if ([backView isKindOfClass:[CardSlot class]]) {
    CardSlot *cardSlot = (CardSlot *)backView;
    // From here you can access cardSlot.slotIndex
}

To help the compiler, you need to cast the pointer to a CardSlot after determining that it is one. This way the compiler can know about the slotIndex property. For example:

if ([backView isKindOfClass:[CardSlot class]]) {
    CardSlot *cardSlot = (CardSlot *)backView;
    // From here you can access cardSlot.slotIndex
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文