【问题标题】:PickerView is not dismissing when Done button is pressed按下完成按钮时,PickerView 不会关闭
【发布时间】:2015-09-30 21:00:03
【问题描述】:

我创建了一个文本字段,输入后将打开一个选择器视图,其中包含一个包含完成按钮的工具栏。但是,当按下完成按钮时,选择器视图不会关闭。除了这个,其他一切都按我的意愿工作。我尝试了几个选项都无济于事。请查看并让我知道我缺少什么。

我的代码如下:

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

【问题讨论】:

  • - (void)doneButton:(UIBarButtonItem *)sender 方法在哪里调用?
  • 你试过在 ViewController.h 上添加 吗?而且您也没有发布 -(void)resignFirstResponder 功能。它不调用它,你必须实现你提到的函数@selector。
  • 什么是产品描述?

标签: ios objective-c button uipickerview


【解决方案1】:

.M 文件

Xib 文件采用 Textfield 并通过连接设置委托。

#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];
}

我希望这会对你有所帮助。

【讨论】:

  • 这工作得很好,我确实看到了我所做的错误。感谢您修复,我正在路上看看我还能在这个项目中搞砸什么!
猜你喜欢
  • 2012-02-10
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多