come mostrare UIPickerView nel UITextField non la tastiera?
-
05-09-2019 - |
Domanda
Voglio mostrare un UIPickerView su come diventare firstResponder di un'UITextField non la tastiera e riempire il valore nel campo di testo formare la vista selettore.
qualcuno conosce questo?
Soluzione
Modifica La risposta è stata corretta al momento della scrittura. Apple ha introdotto dal inputView
. Mafonya rispondere è ciò che si dovrebbe fare al giorno d'oggi.
E 'possibile evitare che la tastiera dal popping up. Impostare la classe per essere il delegato del UITextField: textField.delegate = self
e aggiungere: <UITextFieldDelegate>
dopo la vostra dichiarazione di interfaccia (prima del {) nel file di intestazione.
Ora implementare textFieldShouldBeginEditing:
:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Show UIPickerView
return NO;
}
Se si vuole essere in grado di nascondere la vostra pickerView questo metodo non funziona. Che cosa si potrebbe fare allora è creare sottoclasse di UITextField e ignorare i selettori *trackingWithTouch:event:
e fare la tua magia a quelli. Probabilmente ancora bisogno di tornare NO da textFieldShouldBeginEditting:
per evitare che la tastiera dal mostrare.
Altri suggerimenti
Usa [textfield setInputView: pickerView];
Sostituzione delle Visualizzazioni ingresso del sistema
inputView
inputAccessoryView
Hey ho incollato in un certo codice che ho scritto un po 'indietro per coprire questo scenario. Ci sono due esempi uno con un actionsheet e uno senza un actionsheet:
- (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)];
}
Questo è il codice senza l'actionsheet:
- (void)textFieldDidBeginEditing:(UITextField *)myTextField{
[myTextField resignFirstResponder];
for (int component = 0; component < (((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];
}
Un esempio più dettagliato di questo può essere trovato all'indirizzo: http://www.buggyprogrammer.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/
aggiungi inputview a textField.
picker = [[UIPickerView alloc]init];
[picker setDataSource:self];
[picker setDelegate:self];
[picker setShowsSelectionIndicator:YES];
MyTextField.inputView = picker;
Scopri il codice a (v'è sia TextView + TabBar e pickerView + codice TabBar)
UITextView e UIPickerView con il proprio UIToolbar
In questo modo ottenere il pickerView a dimettersi ecc Tutto u bisogno di fare è quindi utilizzare il metodo pickerView delegato per aggiornare la visualizzazione del testo
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
Invece di utilizzare un campo di testo, utilizzare un UILabel, con uno sfondo bianco. In questo modo la tastiera non mostrerà al tocco. ignorare l'evento tocca l'UILabel, quando ciò accade chiamare il metodo formano la domanda privious che visualizzerà la nuova visualizzazione.
-(void) addToViewWithAnimation:(UIView *) theView