我怎么能只显示选择器并隐藏键盘???

时间:2013-03-29 14:31:24

标签: xcode hide uitoolbar

我正在尝试仅显示选择器并隐藏键盘,但它仅适用于第一个文本字段。

我有一个我制作的文本字段,当我点击第一个文本字段时,选择器弹出窗口,但当我点击其他文本字段时,键盘弹出。

我怎样才能显示选择器?

------------我的代码--------

ViewController.h

interface ViewController : UIViewController <UITextFieldDelegate >{

IBOutlet UIDatePicker *myDatePicker;
UITextField* myTextField;

int x ; int y ; int w ; int h ;  int moretext ;   }   @end

ViewController.m

- (void)viewDidLoad {

 myTextField.delegate = self;    

[super viewDidLoad];

pragma mark - myTextField

x = 36;  y = 50;   w = 36;   h = 25 ;  moretext = 0 ;

for (moretext=0; moretext<7; moretext ++) {

    myTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, w, h)];
    myTextField.textAlignment = NSTextAlignmentCenter;
    myTextField.backgroundColor = [UIColor grayColor];
    myTextField.font = [UIFont fontWithName:@"Helvetica" size:(17)];
    myTextField.layer.borderWidth = 0.70 ;
    myTextField.layer.borderColor =[ UIColor orangeColor].CGColor ;
    myTextField.returnKeyType = UIReturnKeyDone;

          x+=36 ;

    [self.view addSubview:myTextField];

}

pragma mark - myDatePicker

myDatePicker.date = [NSDate date];

[myDatePicker addTarget:self action:@selector(pickerChanged:)    forControlEvents:UIControlEventValueChanged];

myTextField.inputView = myDatePicker;   //link text filed with picker

pragma mark - UIToolbar

UIToolbar* toolBar = [[UIToolbar alloc] init];
toolBar.barStyle = UIBarStyleBlack;
toolBar.translucent = YES;
toolBar.tintColor = nil;
[toolBar sizeToFit];

UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                style:UIBarButtonItemStyleBordered target:self
                                                               action:@selector(pickerHide:)] autorelease];

[toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];

myTextField.inputAccessoryView = toolBar; 

[toolBar release];    }

pragma mark - datePickerChanged

 - (void)pickerChanged:(id)sender{

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

 [formatter setDateFormat:@"h:mm"];

 NSString* myDate = [formatter stringFromDate:myDatePicker.date];

 NSLog(@"date is: %@", myDate);

 myTextField.text = myDate;

 [formatter release];     }

1 个答案:

答案 0 :(得分:0)

我发现我需要的只是将myDatePicker和我的toolBar放在循环中,这就是全部。我的回答我的问题帮助他人。