开发者

Getting value of UIPickerView to UITextField and keep it on the bottom when rotating

Goodday,

I got the following problem:

In my view, i got a texfield, which i am creating in code. The UITextfield is getting build in the code like this:

UITextField *filiaalSelection = [[UITextField alloc] init];
filiaalSelection.frame = CGRectMake(130, 100, 200, 30);
filiaalSelection.borderStyle = UITextBorderStyleLine;
filiaalSelection.delegate = self;

Now when i begin editing the textfield, an UIPickerView needs to popup. I do this in the following c开发者_开发知识库ode:

-(void)textFieldDidBeginEditing:(UITextField *)filiaalSelection {
[filiaalSelection resignFirstResponder];

UIPickerView *myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, scrollView.frame.size.height - 225, scrollView.frame.size.width, 180)];

[myPicker setDelegate:self];
[myPicker setDataSource:self];
myPicker.showsSelectionIndicator = YES;

[self.view addSubview:myPicker];


}

Now i need to do 2 things. The first thing is: Getting the value of my UIPickerView and insert it at the UITextField. The following method returns the current value of the UIPickerView selected:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    [stringsArray objectAtIndex:row];

}

But the problem is, that i don't know how to send the value to the textfield. And i also need a done button above the UIPickerView. What is the best method for this?


Is there any reason you can't just do this?

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    [filiaalSelection setText:(NSString *)[stringsArray objectAtIndex:row]];
}

As for the done button, to make things easy for yourself you should make the picker view the text fields inputView. Then it will be automatically shown and dismissed when the field begins or ends editing (when [textfield resignFirstResponder]; is called).

You can then either add the button to the view somewhere or set it as the text field's inputAccessoryView if you want it to be attached to the top of the picker view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜