按下完成按钮时,PickerView不会消失

时间:2015-09-30 21:00:04

标签: ios objective-c button uipickerview

我创建了一个文本字段,在输入时将打开一个带有包含完成按钮的工具栏的选择器视图。但是,当按下完成按钮时,选择器视图不会被忽略。除此之外,其他所有工作都正如我所希望的那样。我尝试了几种选择无济于事。请查看并告诉我我错过的内容。

我的代码如下:

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

1 个答案:

答案 0 :(得分:1)

  

.M文件

采用Textfield中的Xib文件,并使用connect设置委托。

<td style='width:100px;text-align:left'>
         </td>
         </td>

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