rson
rson

Reputation: 1465

UISegmentedControl Changed Event misfires when adding a method inside

Here is my code for the UIControlEventValueChanged event for a UISegmentedControl:

- (void)segmentAction:(id)sender{

    if([sender selectedSegmentIndex] == 0){     
        pendingIsShowing = YES;
        [freeCutsTable reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
    }else if([sender selectedSegmentIndex] == 1){
        pendingIsShowing = NO;
        [self showAvailableCuts:sender];
        [freeCutsTable reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
    }
}

My problem is when the value is changed, the [self showAvailableCuts:sender] is called, but the segment control no longer changes its index. If I comment out the method call, it works fine...What I tried to do was pass in the segmented control into [self showAvailableCuts:sender] and change it that way, but to no avail...

- (void)showAvailableCuts:(id)sender{
    if(!pendingIsShowing){
        NSString *path =@"https://WebserviceURL";       
        NSString* escapedUrl = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        //[sender setSelectedIndex:1];
        NSLog(@"%@", escapedUrl);
        [self parseXMLFileAtURL:escapedUrl];
    }
}

I'm not sure why this is occurring...

Upvotes: 1

Views: 1199

Answers (1)

NP Compete
NP Compete

Reputation: 2379

Without looking at the code for [self showAvailableCuts:sender]; all I can think of is - why do you need to pass the 'sender' object itself? Just modify the showAvailableCuts method so that you need to pass only the required value(s) like [sender titleForSegmentAtIndex:] , [sender selectedSegmentIndex] etc.

ok, firstly the setter should be [sender setSelectedSegmentIndex:index] not what you have commented out. I have myself successfully used this -

sender.selectedSegmentIndex = index; //in your case index is 1

Upvotes: 1

Related Questions