简体   繁体   English

UIControl子类无法占用目标?

[英]UIControl subclass is unable to take a target?

I've subclasses UIControl and in it I am sending: 我是UIControl的子类,我发送它:

[self sendActionsForControlEvents:UIControlEventValueChanged];

When I create an instance of the object, I add a target as follows: 当我创建对象的实例时,我按如下方式添加目标:

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

The view shows up fine, and without the target being there the functionality works well. 视图显示正常,没有目标在那里功能很好。 But with adding the target, it crashes. 但是添加目标后,它会崩溃。 Any ideas why? 有什么想法吗?

My class is declared with: 我的班级声明:

@interface RMStarRating : UIControl {...}

For what it is worth, I set up my view in - (void)layoutSubviews . 为了它的价值,我在- (void)layoutSubviews设置了我的视图。 Is there another method that I need to subclass in order for the targets to be saved properly or for the targets to be sent the right actions? 是否有另一种方法需要进行子类化以便正确保存目标或者为目标发送正确的操作? I thought UIControl handled saving the targets and actions for you. 我以为UIControl会为您保存目标和操作。

UPDATE: trying to provide more information 更新:尝试提供更多信息

I set the object up as follows: 我将对象设置如下:

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

My sendAction, according to Jordan's suggestion: 根据乔丹的建议,我的sendAction:

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

My function that calls sendActionsForControlEvents: 我的函数调用sendActionsForControlEvents:

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

And the function that should be called (and it is in the header too): 应该调用的函数(它也在标题中):

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

And the log just spits out: 日志只是吐出来的:

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

The debugger has: 调试器具有:

调试器图片

Have you tried implementing 你试过实施吗?

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

instead? 代替? A good example is located here: 一个很好的例子就在这里:

Can I override the UIControlEventTouchUpInside for a UISegmentedControl? 我可以覆盖UISegmentedControl的UIControlEventTouchUpInside吗?

Ok, I figured out what it was. 好的,我知道它是什么。 I was releasing my parent class too soon, so there was no object for the message to be sent back to, even though it was showing on screen. 我过早地发布了我的父类,所以即使它在屏幕上显示,也没有消息被发回的对象。

And I ended up not needing the sendAction:to:forEvent. 我最终不需要sendAction:to:forEvent。

Jordan, thanks you for your help. 乔丹,谢谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM