如何禁用UISegmentedControl?

时间:2011-11-07 18:48:05

标签: iphone objective-c cocoa-touch uibarbuttonitem uisegmentedcontrol

我有以下UISegmentedControl,我想要禁用它:

-(void)displayCheckMark
{
    titleSegmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
    [titleSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"symbolbg.png"] atIndex:0 animated:YES];
    [titleSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"inwatchlist.png"] atIndex:1 animated:YES];
    [titleSegmentedControl addTarget:self action:@selector(titleBarButtonChanged:)forControlEvents:UIControlEventValueChanged];
    titleSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    titleSegmentedControl.frame = CGRectMake(100,0,100,30);
    titleSegmentedControl.momentary = YES;
    titleSegmentedControl.tintColor = [UIColor blackColor];
    self.navigationItem.titleView = titleSegmentedControl;

    [titleSegmentedControl setWidth:60 forSegmentAtIndex:0];
    [titleSegmentedControl setTitle:symbol forSegmentAtIndex:0];
    [titleSegmentedControl setWidth:30 forSegmentAtIndex:1];
    [titleSegmentedControl setEnabled:NO];
}

我没有在代码中的任何地方启用它。然而我仍然可以点击它,它将在titleBarButtonChanged中执行操作:

如何确保无法点击?

3 个答案:

答案 0 :(得分:18)

尝试使用:

- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment;

titleSegmentedControl.userInteractionEnabled = NO;

答案 1 :(得分:3)

titleBarButtonChanged:(id)sender内添加:

if(!sender.enabled) return;

答案 2 :(得分:0)

我为此编写了此方法:

func setEnabledSegmentedControl(enabled: Bool) {
    for segmentIndex in 1..<segmentedControl!.numberOfSegments {
        segmentedControl.setEnabled(enabled, forSegmentAt: segmentIndex)
    }
}