在UIPickerview上显示完成按钮

iOS*_*It. 17 objective-c uipickerview ios

我在viewDidLoad方法中编写了以下代码:

categoryPickerView=[[UIPickerView alloc]init];
categoryPickerView.alpha = 0;
[self.view addSubview:categoryPickerView];
categoryPickerView.delegate=self;
categoryPickerView.tag=1;
Run Code Online (Sandbox Code Playgroud)

并调用此方法来隐藏选择器视图

- (IBAction)hidePickerView:(id)sender {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.6];
    CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
    categoryPickerView.transform = transfrom;
    categoryPickerView.alpha = categoryPickerView.alpha * (-1) + 1;
    [UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我想在选择器视图上显示"完成"按钮,选择器视图应隐藏在按钮单击上.

sat*_*thy 33

你可以使用这段代码,

UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
    style:UIBarButtonItemStyleBordered target:self action:@selector(changeDateFromLabel:)];
toolBar.items = @[barButtonDone];
barButtonDone.tintColor=[UIColor blackColor];
[pickerView addSubview:toolBar];
//(or)pickerView.inputAccessoryView = toolBar;
Run Code Online (Sandbox Code Playgroud)

并设置按钮操作方法 changeDateFromLabel:

-(void)changeDateFromLabel:(id)sender
{
   [[your UI Element] resignFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)

  • 试试这个,对我有用.yourUITextField.inputView = pickerView; yourUITextField.inputAccessoryView = toolBar; toolBar是文本字段的输入附件视图的一部分,而不是选取器视图. (8认同)
  • 我尝试使用pickerView.inputAccessoryView = toolBar; 但它只给出了只读属性@sathiamoorthy的错误赋值 (5认同)
  • 条形图和按钮出现,但按钮不可点击,就像透明的东西覆盖它.而InputAccesoryView是只读的. (4认同)

ana*_*nas 17

您可以使用"完成"按钮创建视图并添加工具栏,并使用UIPickerView作为子视图

- (void)createInputView {
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;

    UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 44)];
    [toolBar setBarStyle:UIBarStyleDefault];
    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

    UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                  style:UIBarButtonItemStyleBordered
                                                                 target:self
                                                                 action:@selector(doneClicked)];
    toolBar.items = @[flex, barButtonDone];
    barButtonDone.tintColor = [UIColor blackColor];

    UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, toolBar.frame.size.height, screenWidth, 200)];
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;


    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, toolBar.frame.size.height + picker.frame.size.height)];
    inputView.backgroundColor = [UIColor clearColor];
    [inputView addSubview:picker];
    [inputView addSubview:toolBar];

    textField.inputView = inputView;
}

- (void)doneClicked {
    [textField resignFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)

  • 这是我猜的正确方法.由于完成按钮不起作用,sathiamoorthy的答案无法正常工作. (2认同)

Kum*_* KL 5

您需要使用 UIToolbar 作为附件视图:试试这个:

#pragma mark - PickerView for Location Selection

- (UIPickerView *)locationsPicker {
    if ( locationsPicker == nil ) {
        locationsPicker = [[UIPickerView alloc] init];
        locationsPicker.delegate = self;
        locationsPicker.dataSource = self;
        locationsPicker.showsSelectionIndicator = YES;
    }
    return locationsPicker;
}

- (UIToolbar *)accessoryView {
    if ( accessoryView == nil ) {
        accessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
                                       UIBarButtonSystemItemDone
                                       target:self
                                       action:@selector(onLocationSelection)];
        [accessoryView setItems:[NSArray arrayWithObject:doneButton]];
    }
    return accessoryView;
}

- (void)onLocationSelection {
    NSInteger row = [self.locationsPicker selectedRowInComponent:0];
    if ( [Location isFirstResponder] ) {
       NSLog(@"%@", [listOfLocations objectAtIndex:row]);
        [Location resignFirstResponder];
    }
}
Run Code Online (Sandbox Code Playgroud)


jun*_*dev 5

复制粘贴解决方案!

这适用于iOS 10(也可能是其他版本)我在9/4/2017写了这段代码.

我需要结合其他答案来获得我想要的东西,这是一个Cancel按钮,Done按钮和一个选择器视图,可以显示/隐藏按钮.忽略我的屏幕截图在选择器中只有一个项目的事实..我的数组中只有一个项目.我测试了多个项目,它运行正常.

在此输入图像描述

免责声明!我已经测试并使用了这段代码.我在我的例子中概括了名字,所以如果我错过了一个并且他们的属性名称没有排列,我会事先道歉.

为了在按钮上显示一个pickerView,你需要创建一个dummyTextField(UITextField在视图控制器的任何地方都是类型.这是因为它UIPickerView的设计与UITextFields一起工作.模仿显示和隐藏选择器,你的按钮点击需要调用dummyTextField becomeFirstResponderresignFirstResponder方法.(例子如下)

第一步

创建自定义数据源类.MyObj是您想要在选择器中显示项目的类.

在.h:

#import <UIKit/UIKit.h>
@class myObj;

@interface PickerDataSource : NSObject <UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, strong) MyObj *selectedObject;
@end
Run Code Online (Sandbox Code Playgroud)

在.m:

#import "PickerDataSource.h"
#import "MyObj.h"

@implementation PickerDataSource

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.selectedObject = mySourceArray[row];
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return mySourceArray.count;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return ((MyObj *)mySourceArray[row]).myTitle;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    int sectionWidth = 300;
    return sectionWidth;
}

@end
Run Code Online (Sandbox Code Playgroud)

第二步在视图控制器中,导入自定义数据源,委托和设置属性:( 必须包含文本字段委托!)

#import "PickerDataSource.h"

@interface MyViewController () <UIPickerViewDelegate, UITextFieldDelegate>
@property (strong, nonatomic) PickerDataSource *pickerDataSource;
@property (strong, nonatomic) UIPickerView *picker;
@property (strong, nonatomic) UITextField *dummyTextField;
@end
Run Code Online (Sandbox Code Playgroud)

第三步

在你的viewDidLoad电话中[self setupPicker];(你接下来会创建这个方法)

第四步

创建 setupPicker

- (void)setupPicker {
    // init custom picker data source
    self.pickerDataSource = [PickerDataSource new];
    // init custom picker
    self.picker = [UIPickerView new];
    // set the picker's dataSource and delegate to be your custom data source
    self.picker.dataSource = self.pickerDataSource;
    self.picker.delegate = self.pickerDataSource;
    self.picker.showsSelectionIndicator = YES;

    // next step is to write this configure method getting called here
    [self configurePickerSubviews];

    // lastly, add the dummyTextField to your view.
    [self.view addSubview:self.dummyTextField];
}
Run Code Online (Sandbox Code Playgroud)

第五步

创建 configurePickerSubviews

- (void)configurePickerSubviews {
    // A UIPickerView must be added as an inputView to a UITextField in order to be displayed on button tap
    // So you need to create a dummyTextField to do so.
    self.dummyTextField = [UITextField new];

    // Create a toolbar to add a done button
    UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,44)];
    [toolBar setBarStyle:UIBarStyleDefault];
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(locationPressed)];

    // Create a flex space so that done button will be right aligned
    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(dismissPicker)];
    toolBar.items = @[cancel, flex, done];
    done.tintColor = [UIColor blackColor];

    [self.picker addSubview:toolBar];

    // Create an input view to add picker + done button as subviews
    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.picker.frame.size.height + 44)];
    [self.picker setFrame:CGRectMake(0, 0, inputView.frame.size.width, inputView.frame.size.height)];
    inputView.backgroundColor = [UIColor clearColor];
    [inputView addSubview:self.picker];
    [inputView addSubview:toolBar];

    // Set custom inputView as container for picker view
    self.dummyTextField.inputView = inputView;

    // Hiding the textfield will hide the picker
    [self.dummyTextField setHidden:YES];
}
Run Code Online (Sandbox Code Playgroud)

第六步

配置它PickerDataSource以便从源阵列中获取要显示的数据.

第7步

点击Run!