2009-11-09 129 views
0

所以,我想事件处理程序附加到小部件,我放在用我的iPhone应用程序:编程连接事件处理程序

 
addTarget:action:forControlEvents 

我添加一个UISegmentedControl在Interface Builder这是通过@property seg暴露在的loadView,我有:

 
- (void)loadView 
{ 
    [ super loadView ] ; 

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

sliderEventIB,只是告诉我们感觉事件:

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

,但我得到的错误是

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

任何想法,它喜欢这里?

回答

2

好像你刚才忘了插入结肠addTarget:

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

它应该是sliderEventIB:不sliderEventIB。

0

那么,UISegmentedControl没有'sliderEventIB'方法。

该方法的'addTarget'部分询问:“一旦事件发生,我该通知谁?”。在这种情况下,您指定应该通知UISegmentedControl,并且应该在该对象上调用sliderEventIB。相反,你应该说

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

1

正确的代码是这样:

- (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"); 
} 

注意,作为使用addTarget:action:forControlEvents注册的方法具有相同的选择。