简体   繁体   中英

change UISegmentedControl, with reference, title

I have segmented control described in XIB (in storyboard exactly) and has reference to controller class. I am trying to change segmented control titles according to language, but I can't change it. When i log my segmented control, it says it don't have any segments, even I have made them in XIB or create programatically. Does anybody know whats wrong?

code:

NSLog(@"segments:%d", [segmentControl numberOfSegments]);
[segmentControl removeAllSegments];
[segmentControl insertSegmentWithTitle:@"stuff" atIndex:0 animated:NO];
[segmentControl insertSegmentWithTitle:@"other stuff" atIndex:1 animated:NO];
NSLog(@"segments:%d", [segmentControl numberOfSegments]);

both logs outputs shows zeroes

- (void)loadView
    {
            [super loadView];
            UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:
                                       [NSArray arrayWithObjects:@"111", @"2222", @"333", nil]];
            seg.frame = CGRectMake(0, 0, 300, 50);
            [seg addTarget:self action:@selector(changed:) forControlEvents:UIControlEventValueChanged];
            [self.view addSubview:seg];
            [seg release];
    }

    - (void)changed:(UISegmentedControl *)segmentControl
    {
            NSLog(@"segments:%d", [segmentControl numberOfSegments]);
            [segmentControl removeAllSegments];
            [segmentControl insertSegmentWithTitle:@"stuff" atIndex:0 animated:NO];
            [segmentControl insertSegmentWithTitle:@"other stuff" atIndex:1 animated:NO];
            NSLog(@"segments:%d", [segmentControl numberOfSegments]);
    }

Your code works fine.

I found that problem was not i code, but in storyboard itself (I played half day with localization)

I found that even I had one storyboard, but it being in en.Iproj made mess (looks like another language couldn't reach it). I took it out to main folder and now everything works again.

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