简体   繁体   中英

UIControl subclass is unable to take a target?

I've subclasses UIControl and in it I am sending:

[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 . 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.

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:

- (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:

- (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?

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.

Jordan, thanks you for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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