sam*_*u_1 3 iphone cocoa-touch objective-c uibutton uicontrol
我在我的界面中使用了10个按钮,并且需要不时地更改按钮的选择器.
我需要使用:
-(void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
Run Code Online (Sandbox Code Playgroud)
在我更改选择器之前或者我可以使用:
-(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
Run Code Online (Sandbox Code Playgroud)
我很担心,如果我使用addTarget改变选择:方法SAN的removeTarget:方法,我会基本上是"堆起来"我的UIButton被按下时,它开火选择.
Mad*_*dav 12
是的,在将新目标分配给按钮之前,应始终删除先前添加的目标.像这样 - -
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(50, 50, 200, 50)];
[btn setTag:101];
[btn addTarget:self action:@selector(method1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
btn = (UIButton *)[self.view viewWithTag:101];
[btn removeTarget:self action:@selector(method1) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:@selector(method2) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
现在,如果你这样做
btn = (UIButton *)[self.view viewWithTag:101];
[btn addTarget:self action:@selector(method2) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
然后将调用方法method1和method2.
希望这可以帮助.
归档时间: |
|
查看次数: |
6616 次 |
最近记录: |