如何禁用部分UISegmentedControl?

时间:2011-06-15 09:58:16

标签: iphone ios uisegmentedcontrol

以下是UISegmentedControl

的代码
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects: [UIImage imageNamed:@"down.png"], [UIImage imageNamed:@"dList.png"], nil]];  

[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];  
segmentedControl.frame = CGRectMake(0, 0, 90, 65);  
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;  
segmentedControl.momentary = YES;  

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];  

[segmentedControl release];  

self.navigationItem.rightBarButtonItem = segmentBarItem;  

[segmentBarItem release];    

- (void)segmentAction:(id)sender{
    if([sender selectedSegmentIndex] == 0)
    {
        button.hidden=NO;
    }
    else
    {
        [self dListMethod ];
    }
}

在if()部分,我要禁用selectedSegmentIndex==0并在button.hidden=YES

时启用

2 个答案:

答案 0 :(得分:142)

使用setEnabled:forSegmentAtIndex:方法启用和禁用细分。

[segmentedControl setEnabled:NO forSegmentAtIndex:0];

如果要禁用第一个段。

答案 1 :(得分:17)

对于那些对 swift 命令感兴趣的人:

Swift 2.3

segmentedControl.setEnabled(false , forSegmentAtIndex: 0);

Swift 3

self.segmentedControl.setEnabled(false, forSegmentAt: 0);