更改UISegmentedControl,带参考,标题

时间:2012-08-02 09:52:28

标签: objective-c ios uisegmentedcontrol

我在XIB中描述了分段控件(完全在故事板中)并且引用了控制器类。我试图根据语言改变分段控制标题,但我不能改变它。当我记录我的分段控件时,它表示它没有任何段,即使我已经在XIB中创建它们或以编程方式创建它们。有人知道什么是错的吗?

代码:

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]);

两个日志输出都显示为零

2 个答案:

答案 0 :(得分:0)


    - (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]);
    }

您的代码运行正常。

答案 1 :(得分:0)

我发现问题不是我的代码,而是故事板本身(我用本地化玩了半天)

我发现即使我有一个故事板,但它在en.Iproj中变得一团糟(看起来像另一种语言无法达到它)。我把它拿到了主文件夹,现在一切都恢复了。