0

So, I'm trying to programmatically attach event handlers to widgets I've placed on my iphone application using:

addTarget:action:forControlEvents

I have added a UISegmentedControl in Interface Builder which is exposed through @property seg and in loadView, I have:

- (void)loadView
{
  [ super loadView ] ;

  //k after that attach our own event handlers
  [ seg addTarget:seg action:@selector(sliderEventIB) forControlEvents:UIControlEventAllEvents ];
}

sliderEventIB, just tells us it feels the event:

-(IBAction)sliderEventIB:(id)sender forEvent:(UIEvent*)event
{
  puts( "I feel you joanna" ) ;
}

but the error I'm getting is

ViewControllersTest[6744:207] *** -[UISegmentedControl sliderEventIB]:
unrecognized selector sent to instance 0x3b21b30

Any idea what it doesn't like here?

3 Answers 3

2

It seems like you just forgot to insert the colon in addTarget:

[ seg addTarget:seg action:@selector(sliderEventIB:) forControlEvents:UIControlEventAllEvents ];

It should be sliderEventIB: not sliderEventIB.

1

The proper code is as such:

- (void)loadView
{
    [super loadView];
    [seg addTarget:self action:@selector(sliderEventIB:forEvent:) forControlEvents:UIControlEventAllEvents];
}
- (IBAction)sliderEventIB:(id)sender forEvent:(UIEvent*)event
{
    NSLog(@"I feel you joanna");
}

Notice that the method has the same selector as is registered using addTarget:action:forControlEvents.

0

Well, the UISegmentedControl doesn't have the 'sliderEventIB' method.

The 'addTarget' section of the method asks: "who do I inform once an event occurs?". In this case, you specified that the UISegmentedControl should be informed and it should call sliderEventIB on that object. Instead, you should say

[seg addTarget:self action:@selector(sliderEventIB) forControlEvents: UIControlEventAllEvents]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.