Programmatically attaching event handlers
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:
- (开发者_如何学Govoid)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?
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.
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.
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]
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论