ios 如何禁用 UISegmentedControl 的一部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6355968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable part of UISegmentedControl?
提问by Pooja
Following is the code for 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];
and
和
- (void)segmentAction:(id)sender{
if([sender selectedSegmentIndex] == 0)
{
button.hidden=NO;
}
else
{
[self dListMethod ];
}
}
In the if() section I want to disable selectedSegmentIndex==0
and enable when button.hidden=YES
在 if() 部分中,我想在什么时候禁用selectedSegmentIndex==0
和启用button.hidden=YES
回答by Deepak Danduprolu
Use setEnabled:forSegmentAtIndex:
method to enable and disable the segments.
使用setEnabled:forSegmentAtIndex:
方法来启用和禁用段。
[segmentedControl setEnabled:NO forSegmentAtIndex:0];
if you want to disable the first segment.
如果要禁用第一个段。
回答by Victor Rius
For those who can be interested in the swiftcommand:
对于那些可能对swift命令感兴趣的人:
Swift 2.3
斯威夫特 2.3
segmentedControl.setEnabled(false , forSegmentAtIndex: 0);
Swift 3
斯威夫特 3
self.segmentedControl.setEnabled(false, forSegmentAt: 0);