CodeGeek123
CodeGeek123

Reputation: 4501

UISegmentedControl Not functioning

Hi im trying to use segmented control to swap between three map views however its not working.

My IBAction method is as follows.

- (IBAction)segmentSwitch:(id)sender {

    NSLog(@"inside segmented switch");
    NSLog(@"selected segment %@",selectedSegment);
    if (selectedSegment == 0) {
        mapView.mapType = MKMapTypeStandard; 

    }
    else{
        mapView.mapType = MKMapTypeHybrid; 

    }
}

I have declared UISegementedControl as an outlet and connected it to the xib view. I have also connected this method with touch down/touch up inside/outside. It still doesn't print the NSLog commands given above. Which means this method is not accessed at all?

Upvotes: 4

Views: 5578

Answers (3)

whyoz
whyoz

Reputation: 5246

Quick summary of how to set up a UISegmentedControl in IB for those dealing with more than two segments:

  1. IBOutlet UISegmentedControl *segmentControl; in @interface (or set it as @property)
  2. - (IBAction)segmentedControlIndexChanged:(id)sender; in .h before @end
  3. drag "Segmented Control" into view and change "Style" to Plain, Bordered, or Bar
  4. increment # of "Segments"
  5. Choose "Segment" and edit the "Title" making sure "Enabled" is checked
  6. Connect your segmentedControl to Files Owner
  7. Connect your segmentedControlIndexChanged: action and select "Value Changed" NOT "Touch up Inside"!!
  8. add some code, maybe a switch statement if you have say 4 segments:

-(IBAction)segmentedControlIndexChanged:(id)sender {

NSLog(@"segmentedControlIndexChanged");

switch (segmentControl.selectedSegmentIndex)
{
    case 0:
    {
        NSLog(@"dateSegmentActive");
        dateSegmentActive = YES;
        noteSegmentActive = NO;
        typeSegmentActive = NO;
        userIDSegmentActive = NO;
        [yourTable reloadData];
    }
        break;
    case 1:
    {
        NSLog(@"noteSegmentActive");
        dateSegmentActive = NO;
        noteSegmentActive = YES;
        typeSegmentActive = NO;
        userIDSegmentActive = NO;
        [yourTable reloadData];
    }
        break;
    case 2:
    {
        NSLog(@"typeSegmentActive");
        dateSegmentActive = NO;
        noteSegmentActive = NO;
        typeSegmentActive = YES;
        userIDSegmentActive = NO;
        [yourTable reloadData];
    }
        break;
    case 3:
    {
        NSLog(@"userIDSegmentActive");
        dateSegmentActive = NO;
        noteSegmentActive = NO;
        typeSegmentActive = NO;
        userIDSegmentActive = YES;
        [yourTable reloadData];
    }
        break;

    default:
        break;
}
}

In recent iOS versions, you need the braces for each case: or you will get errors. This also shows some bool flagging to keep track of what segment is active, maybe for your willDisplayCell method.

Upvotes: 4

rckoenes
rckoenes

Reputation: 69499

You should use the ValueChanged action for detecting the the switch of segments.

Is selectedSegment your UISegmentedControl?

Then you code should be like:

- (IBAction) segmentSwitch:(id)sender {
    if (self.selectedSegment.selectedSegmentIndex == 0) {
        mapView.mapType = MKMapTypeStandard; 
    } else{
        mapView.mapType = MKMapTypeHybrid; 
    }
}

Upvotes: 1

Parth Bhatt
Parth Bhatt

Reputation: 19479

Hope you have selected the right method ValueChanged and also you have connected the outlet of your method properly.

The only thing you need to do now is to replace your code with your code.

- (IBAction)segmentSwitch:(UISegmentedControl *)sender 
{
    NSLog(@"inside segmented switch");
    NSLog(@"selected segment %d",sender.selectedSegmentIndex);
    if (sender.selectedSegmentIndex == 0) 
    {
        mapView.mapType = MKMapTypeStandard; 

    }
    else
    {
        mapView.mapType = MKMapTypeHybrid; 
    }
}

Try replacing this code with your code.

Hope this helps you.

Upvotes: 1

Related Questions