简体   繁体   English

分段控制器隐藏段0

[英]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. 但是我仍然希望晚餐在索引2和深夜在索引3处。所以我所拥有的代码不需要多次更改,具体取决于索引的数量。 I am aware of [segmentedControl setEnabled:NO forSegmentAtIndex:0]; 我知道[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? 基本上有一种方法我可以隐藏午餐指数,以便晚餐仍然在索引2,和/或设置晚餐指数2,即使没有午餐段?

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 ... ...并初始化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. 一种选择是为每个可能的段定义一个ivar,并在构建和调整分段控件时将其设置为相应的段索引。

Lets say you have four possible segments - breakfast, lunch, dinner, and late night. 假设你有四个可能的部分 - 早餐,午餐,晚餐和深夜。 Add these four ivars: 添加这四个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. 现在,当您构建分段控件时,请设置这四个ivars以匹配相应段的实际段索引。 Assign -1 to the ivar if the segment isn't visible. 如果段不可见,则将-1分配给ivar。

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. 如果您在应用运行时添加,删除或重新排序细分,只需更新四个ivars,其余代码将做正确的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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