简体   繁体   中英

segmented controller hide segment 0

I have a segmented controller that I have made using the interface builder it looks like this

在此输入图像描述

Sometimes I set it to be four using this

[segmentedControl insertSegmentWithTitle:@"Dinner" atIndex:2 animated:YES];
[segmentedControl insertSegmentWithTitle:@"Latenight" atIndex:3 animated:YES];

Which works and is fine, but here is where my problem comes up, sometimes I just want to have breakfast and dinner and late night but no lunch. However I still want dinner to be at index 2 and late night at index 3. So the code I have doesn't need to change multiple times depending on how many indexes there are. I am aware of [segmentedControl setEnabled:NO forSegmentAtIndex:0]; But it doesn't look good and is not what I am trying to do?

Basically is there a way I can hide the lunch index so that dinner is still at index 2, and/ or set the dinner to index 2 even though there is no lunch segment?

Thanks for the help in advance!!! :)

EDIT

-(IBAction)selectMeal:(id)sender{


        switch (((UISegmentedControl *) sender) .selectedSegmentIndex) {
            case 0:
                if ([dayInfo isEqualToString:@"Monday"]) {
                    deliString = @"//day[@name='monday']/meal[@name='LUNCH']/counter[@name='Deli']/dish/name";
                }
             case 1:if ([dayInfo isEqualToString:@"Monday"]) {
                    deliString = @"//day[@name='monday']/meal[@name='Dinner']/counter[@name='Deli']/dish/name";
                }

Tried this:

        indexBreakfast = -1;
        indexLunch = 0;
        indexDinner = 1;
        [segmentedControl removeSegmentAtIndex:indexBreakfast animated:YES];
        [segmentedControl insertSegmentWithTitle:@"Dinner" atIndex:indexDinner animated:YES];

Ew please don't use that approach. I know you want to be able to know which type corresponds with which index without a lookup, but it's just lazy.

Here's another approach. When you determine which options you are going to display, store an array of the options (either wrapped enum values, the string titles, whatever). When your target action is fired, grab the type from the array at the index of the selected segment. Done.

In your class, make a property to store the order

@property (nonatomic, strong) NSArray *mealStrings;

Then when you have determined which meals you want and the order, make an array with the values

self.mealStrings = @[@"Breakfast", @"Lunch"];

... and initialize the UISegmentedControl ...

self.segmentedControl = [[UISegmentedControl alloc] initWithItems:self.mealStrings];

Then when you need to know which meal, you can access the type with

NSString *mealName = self.mealStrings[self.segmentedControl.selectedSegmentIndex];

There is no way to hide a segment. It must either be removed or disabled. And since you want to remove the segment, you need a good way to deal with segment indexes having different meaning depending on the visible segments.

One option would be to define an ivar for each possible segment and set it to the corresponding segment index as you build and adjust the segmented control.

Lets say you have four possible segments - breakfast, lunch, dinner, and late night. Add these four ivars:

NSInteger indexBreakfast, indexLunch, indexDinner, indexLateNight;

Now when you build the segmented control, set these four ivars to match the actual segment index of the corresponding segment. Assign -1 to the ivar if the segment isn't visible.

Now your segment handling code can be like:

-(IBAction)selectMeal:(UISegmentedControl *)control {
    NSInteger index = control.selectedSegmentIndex;

    if (index == indexBreakfast) {
        // handle breakfast
    } else if (index == indexLunch) {
        // handle lunch
    } else if (index == indexDinner) {
        // handle dinner
    } else if (index == indexLateNight) {
        // handle late night
    }
}

If you add, remove, or reorder segments as the app runs, simply update the four ivars and the rest of the code will do the right thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM