4

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:

debugger picture

RyanJM
  • 7,028
  • 8
  • 60
  • 90
  • Why does it crash? What line of code? What does the call stack say? It can be difficult to speculatively diagnose crashes. – Shaggy Frog Oct 21 '10 at 22:37
  • I don't know why it crashed, that is what I'm trying to find out. It crashes on sendActionsForControlEvents: (unless I implement Jordan's answer, in which case it crashes during it). Something is wrong between the connection of the UIController and my class, I think. And the Log doesn't spit anything out when it dies. – RyanJM Oct 21 '10 at 23:38

2 Answers2

0

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?

Community
  • 1
  • 1
Jordan
  • 21,746
  • 10
  • 51
  • 63
  • Thanks for that link. Unfortunately passing that onto super didn't work. sendAction:to:forEvent did gets called, but still crashes. – RyanJM Oct 21 '10 at 23:36
  • Then you have another problem. Post the crash log, at least, and the code with sendAction, and setup. – Jordan Oct 22 '10 at 00:13
  • I've posted more information, I hope that helps. By the crash log you mean the development log right? – RyanJM Oct 22 '10 at 14:51
  • I added an image of what the debugger has. – RyanJM Oct 22 '10 at 15:17
  • @RyanJM Try changing the event to UIControlEventAllEvents and see if it fires. I created a template project, added RMStarRating, added target and my starRatingChanged event fired. Without the sendAction. – Jordan Oct 22 '10 at 16:04
0

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.

RyanJM
  • 7,028
  • 8
  • 60
  • 90
  • lol - see, it helps if you post all of the code, instead of snippets. Hard to tell what's really going on. Glad you figured it out. – Jordan Oct 22 '10 at 16:13