文本框选择器

时间:2012-07-29 11:15:26

标签: objective-c ios textbox uipickerview

我正在练习使用选择器将值输入到文本字段中。我试图调整这里找到的一些代码,以便在触摸文本框以填充文本字段时显示选择器。文本框是用代码SOURCE构建的,我想在IB中构建文本框。但是这已停止了演示文稿选择器视图,而现在只显示键盘

也许有人会在这里指出我正确的方向。我的代码:

·H

#import <UIKit/UIKit.h>

@interface pick2 : UIViewController

<UIPickerViewDelegate, UIPickerViewDataSource> {
UIPickerView *locationsPicker;
UIToolbar    *accessoryView;

UITextField *text1;


NSArray      *locations;
}

@property (nonatomic, readonly) UIPickerView *locationsPicker;
@property (nonatomic, readonly) UIToolbar *accessoryView;
@property (strong, nonatomic) IBOutlet UITextField *text1;

- (void)onLocationSelection;

@end

的.m

#import "pick2.h"

@interface pick2 ()

@end

@implementation pick2
@synthesize text1;

- (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:UIBarButtonItemStylePlain
                                                                                   target:self
                                                                                   action:@selector(onLocationSelection)];
    [accessoryView setItems:[NSArray arrayWithObject:doneButton]];
}

return accessoryView;
}

 #pragma mark - Memory management



  - (void)didReceiveMemoryWarning
  {
 [super didReceiveMemoryWarning];
  }

#pragma mark - View lifecycle



- (void)onLocationSelection {
NSInteger row = [self.locationsPicker selectedRowInComponent:0];

( [text1 isFirstResponder] ); {
    text1.text = [locations objectAtIndex:row];
    [text1 resignFirstResponder];
}
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - picker view delegate/datasource

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

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

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


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

 - (void)viewDidLoad
 {

locations = [[NSArray alloc] initWithObjects:@"New York", @"Chicago", @"Memphis",  @"California", @"Seattle",@"London",@"Paris", nil]; 


[super viewDidLoad];
 // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
[self setText1:nil];
[super viewDidUnload];

}


@end

2 个答案:

答案 0 :(得分:0)

您所要做的就是将UITextField的inputView作为挑选者视图的对象。

text1.inputView  = locationsPicker;

点击UITextField将立即显示选择器视图。

答案 1 :(得分:0)

使用IB构建文本框和

对此进行排序

NSInteger selectedRow = [select selectedRowInComponent:0]; text1.text = [arrStatus objectAtIndex: selectedRow]; [text1 resignFirstResponder];

要检索文字值