Objective C、iOS,如何将几个 int 变量附加到属于 IBOutletCollection 一部分的 UIImageView?

发布于 2024-11-08 16:19:42 字数 822 浏览 3 评论 0原文

我需要跟踪一些可以在屏幕上拖动并位于其他图像视图中的图像视图。例如足球进球。我想通过为足球、当前进球和进球中看到的时间附加一些额外的属性来做到这一点。

@property (nonatomic, retain) IBOutlet IBOutletCollection(UIImageView) 
    NSArray *multipleFootballs;

我想,我必须创建一个超级类。 虽然我不确定最好的方法是什么?

EDIT3:谢谢尼克​​,但是我如何访问实例属性?

@interface FootballImageView : UIImageView {
    int intCurrentGoal;
}
@property (readwrite) int intCurrentGoal;

@implementation FootballImageView
@synthesize intCurrentGoal;

-(id)init {

    self = [super init];
    if(self) {
        // do your initialization here...
    }
    return self; 
}

@end

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (id football in multipleFootballs) {

    //if ([[touches anyObject] intCurrentGoal] == 0) {
    //if (football.intCurrentGoal == 0) {

I need to keep track of some image views which can be dragged around the screen and located within other images views. For example footballs in goals. I'd like to do this by having some extra properties attached to the footballs, current goal and times seen in the goal.

@property (nonatomic, retain) IBOutlet IBOutletCollection(UIImageView) 
    NSArray *multipleFootballs;

I figure, that I have to create a super class.
Although I'm not sure the best way to do this ?

EDIT3 : Thanks nick, but how do I then access a instance property ?

@interface FootballImageView : UIImageView {
    int intCurrentGoal;
}
@property (readwrite) int intCurrentGoal;

@implementation FootballImageView
@synthesize intCurrentGoal;

-(id)init {

    self = [super init];
    if(self) {
        // do your initialization here...
    }
    return self; 
}

@end

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (id football in multipleFootballs) {

    //if ([[touches anyObject] intCurrentGoal] == 0) {
    //if (football.intCurrentGoal == 0) {

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

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

发布评论

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

评论(2

终止放荡 2024-11-15 16:19:42

应该很简单:

  • 创建一个继承自 UIImageView 的新子类,我们称之为 MyImageView
  • 将您需要的自定义实例变量添加到标题中
  • 选择旧 UIImageViews 的类,在界面生成器(“标识”选项卡)中选择新的 MyImageView
  • 更改 IBOutletCollection(UIImageView) )IBOutletCollection(MyImageView)

-

- (id)init 
{
    self = [super init];
    if(self) {
        // do your initialization here...
    }
    return self; 
}

回复编辑 3

您面临的问题是您在 TouchesBegan 中使用的匿名类型 (id)。放入这样的支票:

for (FootballImageView *football in multipleFootballs) {
    if(football.intCurrentGoal == 0) {
        football.intCurrentGoal++;
    } 
}

Should be easy:

  • Create a new subclass inheriting from UIImageView, let's call it MyImageView
  • Add your custom instance variables you need to the header
  • Choose as class for the old UIImageViews the new MyImageView in interface builder (Identity Tab)
  • Change IBOutletCollection(UIImageView) to IBOutletCollection(MyImageView)

-

- (id)init 
{
    self = [super init];
    if(self) {
        // do your initialization here...
    }
    return self; 
}

Reply for edit 3

The problem you are facing is the anonymous type (id) you are using in touchesBegan. Put in a check like this:

for (FootballImageView *football in multipleFootballs) {
    if(football.intCurrentGoal == 0) {
        football.intCurrentGoal++;
    } 
}
圈圈圆圆圈圈 2024-11-15 16:19:42

通常(也是好的)建议是您应该避免在视图对象中保留状态。目标中看到的时间和当前目标等属性应该是数据模型的一部分,而不是附加到视图中。这样做有很多充分的理由,但它们大多归结为两大点:

  1. 将状态与视图混合会使事情变得不必要的复杂。
  2. Cocoa Touch 的设计预期应用程序将采用 MVC 范例。

作为第一点的示例,请考虑当用户从主游戏屏幕切换到更改设置、接听电话等时会发生什么。在我看来,您有三种选择:a)保留所有视图,即使他们没有显示您可以维护应用程序的状态; b) 在释放视图之前保存视图的状态; c) 防止用户退出游戏。 (a) 和 (c) 看起来都不是好主意,如果您采用 MVC 方法,您就可以免费获得 (b)。

相反,创建一个可以代表游戏所有方面的数据模型。您可以使用标准类(如 NSDictionary 和 NSSet),或您自己设计的类(如 Football 和 Pitch),或者(最有可能)组合。然后创建一个游戏控制器,观察相关模型对象的变化并在相应的视图中进行更改。

The usual (and good) advice is that you should avoid keeping state in view objects. Attributes like times seen in goal and current goal should be part of your data model, not attached to your views. There are lots of good reasons for this, but they mostly boil down to two big points:

  1. Mixing state with views will make things unnecessarily complicated.
  2. Cocoa Touch is designed with an expectation that apps will adopt the MVC paradigm.

As an example of the first point, consider what happens when the user switches away from the main game screen to change settings, answer a phone call, etc. As I see it, you have three choices: a) preserve all the views even though they're not being displayed that you can maintain the app's state; b) save the state from the views before releasing the views; c) prevent the user from switching away from the game. Neither (a) nor (c) seem like good ideas, and if you take the MVC approach you get (b) for free.

Instead, create a data model that can represent all the aspects of your game. You can use standard classes like NSDictionary and NSSet, or classes of your own design like Football and Pitch, or (most likely) a combination. Then create a game controller which observes changes to the relevant model objects and makes changes in the corresponding views.

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