ios - 按下“完成”按钮时 PickerView 不会关闭

标签 ios objective-c button uipickerview

我创建了一个文本字段,在输入时将打开一个带有包含完成按钮的工具栏的选择器 View 。但是,当按下完成按钮时,选择器 View 不会关闭。除了这个,其他一切都按我想要的方式工作。我尝试了几种选择都无济于事。请检查并让我知道我遗漏了什么。

我的代码如下:

ViewController.h

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController

{IBOutlet UITextField *productDescription; IBOutlet UIPickerView *productPicker; NSArray *productListArray

ViewController.m

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    -(void)addPickerView{
productListArray = [[NSArray alloc]initWithObjects:
                    @"myArray", nil];

    productDescription.delegate = self;
    [self.view addSubview:productDescription];
    [productDescription setPlaceholder:@"Product Description"];
    productPicker = [[UIPickerView alloc]init];
    productPicker.dataSource = self;
    productPicker.delegate = self;
    productPicker.showsSelectionIndicator = YES;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                               initWithTitle:@"Done" style:UIBarButtonItemStyleDone
                               target:self action:@selector(resignFirstResponder)];
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
                      CGRectMake(50, 320, 50, 50)];
    [toolBar setBarStyle:UIBarStyleBlackOpaque];
    NSArray *toolbarItems = [NSArray arrayWithObjects:
                         doneButton, nil];
    [toolBar setItems:toolbarItems];
    productDescription.inputView = productPicker;
    productDescription.inputAccessoryView = toolBar;
    }

    - (void)viewDidLoad

    {
    [super viewDidLoad];
    [self addPickerView];
    }

    #pragma mark - Text field delegates

    -(void)textFieldDidBeginEditing:(UITextField *)textField

    {
    ([textField.text isEqualToString:@""]);
    }

    #pragma mark - Picker View Data source

    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
    }
    -(NSInteger)pickerView:(UIPickerView *)pickerView
    numberOfRowsInComponent:(NSInteger)component{
return [productListArray count];
    }

    #pragma mark- Picker View Delegate

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:
    (NSInteger)row inComponent:(NSInteger)component{
    [productDescription setText:[productListArray objectAtIndex:row]];
    }

    - (void)doneButton:(UIBarButtonItem *)sender{
    NSLog(@"Done Touched");
    [productPicker setHidden:YES];
    }

    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:
    (NSInteger)row forComponent:(NSInteger)component{
    return [productListArray objectAtIndex:row];
    }

    @end

最佳答案

.M File

Xib 文件获取 Textfield 并使用连接设置委托(delegate)。

#import "YourViewController.h"

@interface YourViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
    UIPickerView *productPicker;
    NSArray *productListArray;
    IBOutlet UITextField *productDescription;

}
@end 

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addPickerView];
}

-(void)addPickerView
{
    productListArray = [[NSArray alloc]initWithObjects:@"myArray",@"Rohit",@"Change",@"Your view", nil];


    [productDescription setPlaceholder:@"Product Description"];
    productPicker = [[UIPickerView alloc]init];
    productPicker.dataSource = self;
    productPicker.delegate = self;
    productPicker.showsSelectionIndicator = YES;

    UIToolbar* toolBar = [[UIToolbar alloc] init];
    toolBar.barStyle = UIBarStyleBlack;
    toolBar.translucent = YES;
    toolBar.tintColor = nil;
    [toolBar sizeToFit];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButton:)];
    [toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];
    productDescription.inputView = productPicker;
    productDescription.inputAccessoryView = toolBar;

}

- (IBAction)doneButton:(id)sender
{
    NSLog(@"Done Touched");

    [productPicker removeFromSuperview];
    [productPicker resignFirstResponder];
    [self.view endEditing:YES];
}

#pragma mark - Text field delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    productDescription.inputView = productPicker;
}

    #pragma mark - Text field delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    productDescription.inputView = productPicker;
}

#pragma mark - Picker View Data source

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [productListArray count];
}

#pragma mark- Picker View Delegate

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    [productDescription setText:[productListArray objectAtIndex:row]];
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [productListArray objectAtIndex:row];
}

我希望这对你有帮助。

关于ios - 按下“完成”按钮时 PickerView 不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32875525/

相关文章:

ios - fatal error : unexpectedly found nil while unwrapping an Optional value(When adding to a array)

android - 如何以编程方式在 Android 的按钮中缩放或自动调整 Drawable Top 的图像?

javascript - 如果 javascript 在 $(document).ready 函数中,它不允许我选择按钮

ios - Swift:尝试从我的应用程序在 Safari 中打开 URL 时出现 'Snapshotting a view that has not been rendered..' 错误

ios - 将 IBAction 连接到自定义类

ios - 行为类似于 iPhone 中的日历应用程序

objective-c - 设置记录数组数据时要调用的类标识符

ios - GridView (UICollectionView)的偏移量随着分页滚动而逐渐变化

Java - 添加输入按钮

ios - 我无法暂停在 tablecell 上播放视频