Yes, you should always delete a previously added target before assigning a new target to a button. Like this ---
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];
now if you do it
btn = (UIButton *)[self.view viewWithTag:101]; [btn addTarget:self action:@selector(method2) forControlEvents:UIControlEventTouchUpInside];
then both the methods method1 and the method will be called.
Hope this helps.
source share