简体   繁体   中英

Monotouch : UIPicker as first responder for a UITextbox

I'm trying to implement a UIPicker as custom 'edit' input for the contents of a UITextField.

So far i have this :

public override void ViewDidLoad ()
{
 // ... snip

    tb_status.EditingDidBegin += delegate {
        //prevent keyboard from popping up
        tb_status.ResignFirstResponder();

        TogglePDCASelect();
    };

    tb_status.ShouldReturn = (textField) => {
        TogglePDCASelect();
        textField.ResignFirstResponder();
        return true;
    };
 // ... snip

    private void TogglePDCASelect()
    {
            pc_PDCAPicker.Hidden = !pc_PDCAPicker.Hidden;
            if(pc_PDCAPicker.Hidden)
        {
                        //AccomodateResponder accomodates the UIScrollView `vwDetails` so that there is room for the responder
                        //and recalculates the scroll content size.
                        //deaccomodate does the reverse.
                scrollViewer.DeAccomodateResponder(vwDetails);
        } 
        else 
        {
            scrollViewer.AccomodateResponder(vwDetails, tb_status.Frame);
        }
    }

So far, so good, but when i tap the textbox that calls the picker, and then tap a normal text field or switch the vwDetails subview, the picker stays in view.

It seems like a lot of pain to set it up like this. Am i doing it wrong? is there any way i can get the picker to behave just like the default firstresponder (keyboard)?

So if I understand correctly, you essentially want to use a custom keyboard/input for that particular textfield?

You can override that UITextField 's InputView with your own custom UIView and it will show that instead of the keyboard when it becomes first responder.

EDIT

Your picker's InputView is going to be null so thats why that didn't work. You need to create a new UIView and assign that to the tb_status 's InputView . So if you add your picker to this view then it should display your picker.

A simple code sample: (I haven't got the time to verify this is 100% correct but should get you on your way)

...// snip
UIView pickerView = new UIView(new RectangleF(0,0,320,216));
pickerView.AddSubview(pc_PDCAPicker);
tb_status.InputView = pickerView;

Lets say your view is 320 x 480. So Initially your picker should have frame as x, 320, width, height.

Now, when user edits the text field, animate the picker frame to some visible height using UIView animation as follows.

-(void) presentPickerView{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];

CGRect viewFrame = self.pickerView.frame;

viewFrame.origin.y -= self.pickerView.bounds.size.height;

self.pickerView.frame = viewFrame;

[UIView commitAnimations];

}

-(void) dismissPickerView {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];

CGRect viewFrame = self.pickerView.frame;

viewFrame.origin.y += self.pickerView.bounds.size.height;

self.pickerView.frame = viewFrame;

[UIView commitAnimations];

}

You don't need to see the keyboard when you edit this textField, so return NO here.

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:myTextField]) {
    [self presentPickerView];
    return NO;
}
return YES;

}

Animate the picker down when text field editing is done.

- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([textField isEqual:myTextField]) {
    // Animate picker down back again.
    [self dismissPickerView];
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM