如何禁用UISegmentedControl?

8
我有一个 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的操作:
如何确保它无法被点击?
2个回答

19

尝试使用:

- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment;
或者
titleSegmentedControl.userInteractionEnabled = NO;

3

titleBarButtonChanged:(id)sender 中添加:


if(!sender.enabled) return;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接