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

标签 objective-c ios uiimageview nsarray

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

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

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

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) {

最佳答案

应该很容易:

  • 创建一个继承自 UIImageView 的新子类,我们称之为 MyImageView
  • 将所需的自定义实例变量添加到 header
  • 在界面构建器(“身份”选项卡)中选择新的 MyImageView 作为旧 UIImageView 的类
  • 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++;
    } 
}

关于objective-c - Objective C、iOS,如何将几个 int 变量附加到属于 IBOutletCollection 一部分的 UIImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6045166/

相关文章:

ios - 将静态内容添加到 UITableViewCell 中

使用 md5 key 的 iOS 3DES 加密

iphone - 在 iOS7 上注入(inject)系统范围的触摸事件

ios - 修改除一个之外的所有 subview

ios - imageview 未相应更新

objective-c - 从 NSString 中删除双引号

ios - 帖子的神秘网络问题。 Wifi 工作,手机不

ios - Swift:如何计算居中字符串的大小

ios - 删除特定部分中的 UITableView 单元格

swift - UIImageView 是 NIL