Ordinato! Ho semplicemente utilizzare una visualizzazione di testo 1x1px e utilizzare il metodo delegato textViewDidChangeSelection:
EDIT: Per iOS 6 ho dovuto modificare la visualizzazione del testo a 50x50px (o almeno abbastanza per visualizzare in realtà il testo) per far funzionare tutto
Sono anche riuscito a sopprimere la tastiera su schermo quando il pedale è disconnesso.
Questo è il mio codice in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
UITextView *hiddenTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[hiddenTextView setHidden:YES];
hiddenTextView.text = @"aa";
hiddenTextView.delegate = self;
hiddenTextView.selectedRange = NSMakeRange(1, 0);
[self.view addSubview:hiddenTextView];
[hiddenTextView becomeFirstResponder];
if (keyboardShown)
[hiddenTextView resignFirstResponder];
keyboardShown
è dichiarata come bool
nel mio file di intestazione.
Quindi aggiungere questi metodi:
- (void)textViewDidChangeSelection:(UITextView *)textView {
/******TEXT FIELD CARET CHANGED******/
if (textView.selectedRange.location == 2) {
// End of text - down arrow pressed
textView.selectedRange = NSMakeRange(1, 0);
} else if (textView.selectedRange.location == 0) {
// Beginning of text - up arrow pressed
textView.selectedRange = NSMakeRange(1, 0);
}
// Check if text has changed and replace with original
if (![textView.text isEqualToString:@"aa"])
textView.text = @"aa";
}
- (void)keyboardWillAppear:(NSNotification *)aNotification {
keyboardShown = YES;
}
- (void)keyboardWillDisappear:(NSNotification *)aNotification {
keyboardShown = NO;
}
Spero che questo codice aiuta a qualcun altro che sta cercando una soluzione a questo problema. Sentiti libero di usarlo.
Funzionerà con 'UIWebView'? – Dmitry
@Altaveron la proprietà keyCommands è una proprietà su UIResponder e UIWebView eredita da UIResponder, quindi non vedo motivi per cui non funzioni. – amergin
'UIWebView' è un contenitore per molte sottoview. – Dmitry