jak wyświetlić pickerview w uitextfield, a nie klawiaturę?

Chcę pokazać UIPickerView, gdy staje się FirstResponder pola UITextfield, a nie klawiatury i wypełnić wartość w polu tekstowym w widoku pickera.

Ktoś to wie?

Author: Rahul Vyas, 2009-05-25

5 answers

Edit: ta odpowiedź była poprawna w momencie pisania. Od tego czasu Apple wprowadziło inputView. Mafonya odpowiedź jest tym, co powinieneś robić w dzisiejszych czasach.

Jest możliwe, aby zapobiec pojawianiu się klawiatury. Ustaw klasę jako delegata UITextField: textField.delegate = self i dodaj: <UITextFieldDelegate> po deklaracji interfejsu (przed {) w pliku nagłówkowym.

Teraz zaimplementuj textFieldShouldBeginEditing::

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // Show UIPickerView

    return NO;
}

Jeśli chcesz mieć możliwość ukrycia pickerView ta metoda nie praca. Możesz wtedy utworzyć podklasę UITextField i nadpisać selektory *trackingWithTouch:event: i wykonać w nich swoją magię. Prawdopodobnie nadal musisz zwrócić NO z textFieldShouldBeginEditting:, aby uniemożliwić wyświetlenie klawiatury.

 25
Author: klaaspieter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:09:14

Use [textfield setInputView: pickerView];

Zastępowanie widoków wejściowych systemu
inputView
inputAccessoryView

 28
Author: mafonya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-20 15:51:55

Hej wkleiłem jakiś kod, który napisałem jakiś czas temu, aby opisać ten scenariusz. Istnieją dwa przykłady, jeden z arkuszem działań i jeden bez arkusza działań:

- (void)textFieldDidBeginEditing:(UITextField *)myTextField{

             [myTextField resignFirstResponder];

             actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

             [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

             CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

             UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];

              pickerView.showsSelectionIndicator = YES;

              pickerView.dataSource = self;

              pickerView.delegate = self;

              [actionSheet addSubview:pickerView];

              [pickerView release]; //NB this may result on the pickerview going black the next time you come back to the textfield, if this is the case just remove this statement

              UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"SUBMIT", @"")]];

              closeButton.momentary = YES;

               closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);

               closeButton.segmentedControlStyle = UISegmentedControlStyleBar;

               closeButton.tintColor = [UIColor blackColor];

               [closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];

               [actionSheet addSubview:closeButton];

               [closeButton release];

               [actionSheet showInView:displayedInView];

               [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

}

To jest kod bez arkusza działania:

- (void)textFieldDidBeginEditing:(UITextField *)myTextField{

         [myTextField resignFirstResponder];

          for (int component = 0;  component &lt; (((NSInteger)numberOfComponentsForPickerView) - 1); component++) {

              NSInteger valueForRow = [[self.textField.text substringWithRange:NSMakeRange(component,1)] integerValue];

               [pickerView selectRow:valueForRow inComponent:component animated:YES];

          }

           [self fadeInPickerView];

            [view addSubview:pickerView];

            [pickerView release];

}

Bardziej szczegółowy przykład tego można znaleźć na stronie: http://www.buggyprogrammer.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/

 4
Author: williamb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-28 08:41:08

Dodaj inputview do textField.

picker = [[UIPickerView alloc]init];
[picker setDataSource:self];
[picker setDelegate:self];
[picker setShowsSelectionIndicator:YES];

MyTextField.inputView = picker;
 4
Author: oscar castellon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-16 08:20:26

Sprawdź kod na (jest tam zarówno textview + tabbar, jak i pickerview+tabbar code)

UITextView i UIPickerView z własnym UIToolbar

To sprawi, że pickerview zrezygnuje itp. Wystarczy użyć metody delegata pickerview, aby zaktualizować widok tekstowy

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

Zamiast używać pola tekstowego, użyj etykiety UIlabel z białym tłem. W ten sposób klawiatura nie pojawi się po dotknięciu. nadpisuje Zdarzenie dotyka na UILabel, kiedy to happens wywołanie metody z privious pytanie, które wyświetli nowy widok.

 -(void) addToViewWithAnimation:(UIView *) theView
 1
Author: Bluephlame,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:00:10