如何在使用UIPickerView时禁用视图

时间:2012-10-22 09:41:39

标签: iphone objective-c ios uiview uipickerview

我一直在使用UIPickerView,我的问题很简单,如何在选择器视图显示时禁用视图,这样我们就可以确保用户不会在视图中更改任何内容。我尝试使用setuserInteractinEnabled:方法,但它也禁用了选择器视图..任何想法..?

7 个答案:

答案 0 :(得分:1)

我必须做类似的事情,并将UIActionSheet子类化为其添加选择器视图。虽然操作表已启动,但您无法与基础视图进行交互。

您可以在此处找到一些示例,了解如何将UIPickerView添加到UIActionSheet,例如how to add UIPickerView in UIActionSheet

答案 1 :(得分:1)

您有几种选择:

  1. 如果您使用的是Ipad,请在弹出窗口中显示选择器:Popover
  2. 如果您使用的是Iphone,请在操作表(UIActionSheet)中显示:Action sheet
  3. 使用现有的模态视图控制器
  4. 显示选择器

    这3个选项可以阻止父视图触摸事件。

答案 2 :(得分:1)

将一个UIView命名为viewBack,如下所示

@interface ViewController : UIViewController{
    UIView *viewBack;
}
viewDidLoad:方法中的

只是定义了这个viewBackwith框架,即

- (void)viewDidLoad
{
        viewBack = [[UIView alloc] initWithFrame:CGRectMake(95, 230, 130, 40)];
        viewBack.backgroundColor = [UIColor blackColor];
        viewBack.alpha = 0.7f;
        viewBack.layer.masksToBounds = NO;
        viewBack.layer.cornerRadius = 8; 
        viewBack.hidden =YES;
}

当你想在那个方法显示屏幕上显示UIPickerView时,就像这样..

-(IBAction)btnPickerViewOpen_Clicked:(id)sender{ 
         viewBack.hidden = NO;
        [self.view bringSubviewToFront:viewBack];
        [self.view bringSubviewToFront:yourPickerView];
}

当你想隐藏当时的UIPickerView时,请使用以下流程...

-(IBAction)btnPickerViewClosed_Clicked:(id)sender{ 
         viewBack.hidden = YES;
        // also hide pickerview with your requirement
}

我希望这可以帮助你...

答案 3 :(得分:0)

我通常添加50%透明度和黑色背景的额外UIView - 这解决了三个问题:

  • 自定义视图(选择器视图或任何其他用户输入字段)
  • 背景变暗 - 以便用户注意到需要做什么
  • 用户互动仅适用于选择器视图/文本输入/等

答案 4 :(得分:0)

你只需要尝试@Rene Jennrich的答案..你需要看一些例子......如果你想要这个代码..

    UIActionSheet *inputActionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                   delegate:nil
                                          cancelButtonTitle:nil
                                     destructiveButtonTitle:nil
                                          otherButtonTitles:nil];
    [inputActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];
    [pickerToolbar sizeToFit];
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent;

    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelButtonPressed:)];
    [barItems addObject:cancelBtn];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [barItems addObject:flexSpace];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonPressed:)];
    [barItems addObject:doneBtn];
    [pickerToolbar setItems:barItems animated:YES];
    [inputActionSheet addSubview:pickerToolbar];

    UIPickerView fontPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, 0, 0)];
    fontPickerView.showsSelectionIndicator = YES;
    [fontPickerView setTag:1];
    [fontPickerView setDelegate:self];
    [fontPickerView setDataSource:self];
    [inputActionSheet addSubview:fontPickerView]; 
    [inputActionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
    [inputActionSheet setBounds:CGRectMake(0, 0, 320, 485)];

答案 5 :(得分:0)

我有同样的问题。当我轻按内部的pickerview时,UIPickerView后面的按钮会起作用,尽管按钮不可见,但只有pickerview是可见的。我已经使用按钮操作视图的hidden属性来管理它。喜欢

  @IBAction func tapPrivacyPolicy(_ sender: Any) {
    if viewDatePicker.isHidden == false { // means UIPickerView is Visible
          return
    }
   //do other work 
}

答案 6 :(得分:0)

要禁止单击选择器,请执行以下操作:

m