Anton
Anton

Reputation: 45

Segment control doesn't change button

I have code of segment control:

-(IBAction)sectionswitch:(id)sender {

    if (control.selectedSegmentIndex == 0) {
        [button addTarget:self action:@selector(call1:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"+000000000000" forState: UIControlStateNormal] ;
    }

    if (control.selectedSegmentIndex == 1) {
        [button addTarget:self action:@selector(call2:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"+111111111111" forState: UIControlStateNormal] ;
    }

}


- (IBAction)call1:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://+000000000000"]];
}

- (IBAction)call2:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://+111111111111"]];
}

But when i change segment control button always call to +111111111111 How to fix?

Upvotes: 0

Views: 89

Answers (1)

Jasarien
Jasarien

Reputation: 58448

Each time you toggle the segmented control your code adds a target/action to the button. Note it adds, not replaces.

So when you toggle from +000 to +111 and back, the button now has both target/actions added, and will call both call1: and call2: methods.

Before adding the target/action you should remove the previous target/action added to the button with removeTarget:action:forControlEvents:.

Upvotes: 1

Related Questions