objective-c:当我创建多个自定义类实例时,委托对象参数被覆盖

时间:2013-01-08 20:05:00

标签: iphone objective-c xcode

编辑:我为浪费时间道歉,erorr与我正在采取的行动无关,而是我的代码中的一些逻辑让我相信这是原因。我使用他的想法传递整个AuthorSelectionView,以及他关于纠正NSNumer错误的说明,我正在给Kevin回答正确答案。对此感到抱歉。

我一直试图想出这个问题好几个小时,甚至连一天都没关系,但仍然无法理解......

我的情况如下:

我已经创建了一个实现'UIView'的自定义类,并将此类变为协议,如下所示:

自定义UIView h文件

@protocol AuthorSelectionViewDelegate <NSObject>
-(void)AuthorSelected:(NSNumber *)sender;
@end

#import <UIKit/UIKit.h>

@interface AuthorSelectionView : UIView

@property (nonatomic,assign) id<AuthorSelectionViewDelegate> delegate;

@property (strong,retain) NSNumber *authorID;

- (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl withID:(int)authorID ;

@end

实施......

    - (id)initWithFrame:(CGRect)frame withImage:(UIImage *)img withLabel:(NSString *)lbl withID:(int)authorID
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.authorID = [[NSNumber alloc] initWithInt:authorID]; //used to distinguish multiple instances of this class in a view.

            ...

        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)];
        [button addTarget:self action:@selector(CUSTOMBUTTONCLICK) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
return self;
}

    - (void) CUSTOMBUTTONCLICK
    {
        [self.delegate performSelector:@selector(AuthorSelected:) withObject:self.authorID];
    }

现在我的委托对象中的方法被调用得很好,但我的主要问题是,当我有多个AuthorSelected类的实例已分配。 > ..(NSNumber authorID)。我有一些奇怪的行为。价值传递似乎几乎是随机的,但我检测到一些模式,其中传递的价值即将到来。

那令人困惑如此恶心试图解释: 我创建了AuthorSelected视图的两个实例,一个是authorID = 1,另一个是authorID = 2.

在第一次按下时,让我说按第一个按钮,我会按预期得到1。

在第二次按下时,如果我按下第一个自定义按钮,我会得到'1',但如果我按下第二个,我仍然会得到1.

第三步,任何一个按钮都会让我回到'2'

我觉得这是指针的一些问题,因为这对我来说一直是一个弱点,但任何帮助都会非常感激,因为我似乎无法想出这一点。

谢谢!

修改

as requested here is how I create the AuthorSelectionView Objects...

AuthorSelectionView * asView01 = [[AuthorSelectionView alloc]
initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)
withImage:userPic1
withLabel:randomUserName
withID:1];

asView01.delegate = self;

AuthorSelectionView * asView02 = [[AuthorSelectionView alloc]
initWithFrame:CGRectMake(0, 0, FRAMEWIDTH, FRAMEHEIGHT)
withImage:userPic2
withLabel:randomUserName2
withID:2];

asView02.delegate = self;

可能很重要的细节:

只要我点击其中一个自定义视图,我的代码就会设置为(现在)调用运行上面的AuthorSelectionView分配代码的方法,这样我就可以使用相同的布局刷新屏幕,但是userpic /用户名。我知道这是糟糕的设计,但是现在我只想要基本功能工作,然后担心重绘。我提到了这个小窍门,因为我明白,在画布上画画时,我会明白目标-c'层叠'在彼此之上的视图,并且有一种想法,也许当我点击我认为可能是我的第二个按钮时,它真的“点击”了下面的层和拉错信息。

3 个答案:

答案 0 :(得分:1)

您对问题的描述有点令人困惑,但init中的这一行显然是错误的:

self.authorID = [self.authorID initWithInt:authorID];

-init中,您的媒体资源self.authorID默认为nil,因此表达式[self.authorID initWithInt:authorID]相当于[nil initWithInt:authorID],后者的计算结果为nil }。所以你应该在行动中看到nil。你可能想说self.authorID = [NSNumber numberWithInt:authorID]

答案 1 :(得分:0)

而不是:

self.authorID = [self.authorID initWithInt:authorID];

把:

self.authorID = [NSNumber numberWithInt:authorID];

self.authorID = [[NSNumber alloc] initWithInt:authorID];

编辑:

您的代码中是否有错误或警告?我看不到你在init方法中返回self对象(“return self;”)

答案 2 :(得分:0)

你错过了alloc消息,所以这条消息:

self.authorID = [self.authorID initWithInt:authorID]; 

发送到nil目标,因为尚未分配self.authorID。

首先分配它,然后使用init方法,或混合这两个消息。更快的语法允许这样做:

self.authorID= @(authorID);

编辑

我没有看到您初始化委托的位置,如果您尚未初始化该方法,则甚至不应该调用该方法。显示创建AuthorSelectionView对象的代码并设置委托。