简体   繁体   中英

Push to segmented control segment by segment's title

I have a UISegmentedControl that contains 3 segments. Once my application starts, the application automatically starts at segment 1. I would like my application to start at segment 3. I do not want to simply select the index of the segment array like so:

self.segmentedControl.selectedSegmentIndex = 2; 

I would like my view to move to the segment that contains a certain title:

if (self.segmentControl.title isEqualToString:@"SomeTitle") {
     //Do something
}

Is there a way to accomplish this?

No there isn't a way to do this. The closest you could get to it would be to #define the indexes in the class you're using them.

Something like this:

#define SomeTitle_Index 0 
#define SomeOtherTitle_Index 1
#define AnotherTitle_Index 2 

Then selecting them would be like this:

[self.segmentedControl setSelectedSegmentIndex:SomeOtherTitle_Index];

And to check index:

if (self.segmentedControl.selectedSegmentIndex == SomeOtherTitle_Index){
     //Do something 
}

You can explicitly call the segmented control valueChanged action method.

For example, you have a valueChanged action method as

(IBAction)segmentIndexChanged:(UISegmentedControl *)sender {
   if (sender.title isEqualToString:@"SomeTitle") {
     //Do something
   }
}

Then the method call will be

self.segmentedControl.selectedSegmentIndex = 2; 
[self segmentIndexChanged:self.segmentControl];

1)要在界面构建器中选择默认velue,只需取消选中索引的选定属性。

int index=-1;
    for (int i=0; i<3; i++) {
        if ([[segment titleForSegmentAtIndex:i] isEqualToString:@"SomeTitle"]) {
            index=i;
        }
    }
    segment.selectedSegmentIndex=index;

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