我在搜索视图控制器中有一个按钮,它调用以下方法,创建一个UIActionSheet
包含两个UISegmentedControl
s (用于选择搜索设置)。当用户在第一个分段控件上选择某个按钮时,我想禁用(setEnabled:NO
)第二个分段控件。问题是它不会重绘(因此它永远不会被禁用)。如果我最初在创建 UIActionSheet 时 setEnabled:NO,则它最初被正确禁用。
我尝试通过调用强制重绘第二个分段控件和操作表[UIView setNeedsDisplay]
,但这不起作用。
还尝试了第二个分段控件上的 removeFromSuperview 。
内置的 UIActionSheet 无法做到这一点吗?还是有更好的方法让我显示搜索设置?这是一个为 iOS5 编码的 iPhone 应用程序。弹出框不适用于 iPhone。我能想到的另一种方法是让设置显示在它自己的视图中,但我想避免这种情况。
-(void)onSettingsButtonClick:(id)sender
{
// display segmented button views on actionSheet
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Search Settings" delegate:self cancelButtonTitle:@"Close" destructiveButtonTitle:nil otherButtonTitles:nil, nil];
sheet.actionSheetStyle = UIActionSheetStyleDefault;
UISegmentedControl * segFullTextOrTags = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Tags", @"Full Text", @"Users", nil]];
[segFullTextOrTags addTarget:self action:@selector(onSegFullTextOrTagsClicked:) forControlEvents:UIControlEventValueChanged];
[segFullTextOrTags setSegmentedControlStyle:UISegmentedControlStyleBar];
[segFullTextOrTags setTag:100];
UISegmentedControl * segType2 = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"All Actions", @"Discussions", @"Responses", @"Comments", nil]];
[segType2 addTarget:self action:@selector(onSegType2Clicked:) forControlEvents:UIControlEventValueChanged];
[segType2 setSegmentedControlStyle:UISegmentedControlStyleBar];
[segType2 setTag:101];
[sheet showFromTabBar:self.tabBarController.tabBar];
// code that expands the action sheet height and adjusts the view positions is omitted here
// add the additional views to the actionSheet
[sheet addSubview:segFullTextOrTags];
[sheet addSubview:segType2];
}
-(void)onSegFullTextOrTagsClicked:(id)sender
{
[self updateSettingsUI:(UIActionSheet*)sender];
}
-(void)onSegType2Clicked:(id)sender
{
[self updateSettingsUI:(UIActionSheet*)sender];
}
-(void)updateSettingsUI:(UIActionSheet*)actionSheet
{
UISegmentedControl * segFullTextOrTags = (UISegmentedControl*)[actionSheet viewWithTag:100];
UISegmentedControl * segType2 = (UISegmentedControl*)[actionSheet viewWithTag:101];
if (segFullTextOrTags.selectedSegmentIndex == 2) // users
{
// disable 2nd segmented button control
[segType2 setEnabled:NO];
}
else
{
// enable 2nd segmented button control
[segType2 setEnabled:YES];
}
// doesn't work...
[segType2 setNeedsDisplay];
[actionSheet setNeedsDisplay];
}