UIControl子类无法占用目标?

时间:2010-10-21 22:17:55

标签: iphone subclass uicontrol

我是UIControl的子类,我在其中发送:

[self sendActionsForControlEvents:UIControlEventValueChanged];

当我创建对象的实例时,我按如下方式添加目标:

[starView addTarget:self action:@selector(starRatingChanged:) forControlEvents:UIControlEventValueChanged];

视图显示正常,没有目标在那里功能很好。但是添加目标后,它会崩溃。有什么想法吗?

我的课程声明为:

@interface RMStarRating : UIControl {...}

对于它的价值,我在- (void)layoutSubviews中设置了我的观点。是否有另一种方法需要进行子类化以便正确保存目标或者为目标发送正确的操作?我以为UIControl为您保存了目标和行动。

更新:尝试提供更多信息

我按如下方式设置了对象:

RMStarRating *starView = [[RMStarRating alloc] initWithFrame:CGRectMake(10, 70, 23*5, 30)];
[starView addTarget:self action:@selector(starRatingChanged:) forControlEvents:UIControlEventValueChanged];
....
[self.view addSubview:starView];

我的sendAction,根据乔丹的建议:

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    NSLog(@"send action");
    [super sendAction:action to:target forEvent:event];
}

我调用sendActionsForControlEvents的函数:

- (void)updateValue:(UITouch *)touch {
    ....
    NSLog(@"sendActionsForControlEvents");
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

应该调用的函数(它也在标题中):

- (void)starRatingChanged:(id)sender {
    NSLog(@"star rating changed");
}

日志只是吐出来了:

2010-10-22 09:45:41.348 MyApp[72164:207] sendActionsForControlEvents
2010-10-22 09:45:41.350 MyApp[72164:207] send action

调试器具有:

debugger picture

2 个答案:

答案 0 :(得分:0)

您是否尝试过实施

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event

代替?一个很好的例子就在这里:

Can I override the UIControlEventTouchUpInside for a UISegmentedControl?

答案 1 :(得分:0)

好的,我弄清楚它是什么。我过早地发布了我的父类,所以即使它在屏幕上显示,也没有消息被发回的对象。

我最终不需要sendAction:to:forEvent。

乔丹,谢谢你的帮助。