0

我在搜索视图控制器中有一个按钮,它调用以下方法,创建一个UIActionSheet包含两个UISegmentedControls (用于选择搜索设置)。当用户在第一个分段控件上选择某个按钮时,我想禁用(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];
}
4

1 回答 1

1

我认为 UIActionSheet 不是向用户呈现设置视图的正确控件。

从类参考文档中:

使用 UIActionSheet 类为用户提供一组备选方案,用于如何继续执行给定任务。您还可以使用操作表来提示用户确认潜在危险的操作。操作表包含一个可选标题和一个或多个按钮,每个按钮对应于要采取的操作。

相反,您应该创建一个 UIViewController 子类并以模态方式或使用导航控制器呈现它。

我不知道为什么setEnabled在您的代码中不起作用。setNeedsDisplay没有必要,控件应该重绘自己。检查 updateSettingsUI 和 onSettingsButtonClick 中的 segType2 是否是同一个对象。

于 2012-06-18T22:11:40.667 回答