iPhone SDK - Problema incollato nella posizione di testo corrente
-
27-09-2019 - |
Domanda
Sto cercando di incollare il testo direttamente dove si trova attualmente il cursore. Ho cercato di fare quello che dice: - http://dev.ragfield.com/2009/09/insert-ttext-at-current-cursor-line.html
L'accordo principale è che non posso semplicemente andare TextBox1.Text (ecc.) Perché il campo di testo è nel mezzo di una cella personalizzata. Voglio solo avere un po 'di testo aggiunto a dove si trova il cursore (quando premo un tasto personalizzato su una tastiera).
-Mo voglio solo incollare un decimale nella casella di testo ...
L'errore che ricevo è:
2010-05-15 22: 37: 20.797 PageControl [37962: 207 *-[Pasta myDetailController:]: selettore non riconosciuto inviato all'istanza 0x1973d10 2010-05-15 22: 37: 20.797 PageControl [37962: 207] * App terminante a causa dell'eccezione non insegnata "NsInvalidargumentException", motivo: '*** -[pasta myDetailController:]: selettore non riconosciuto inviato all'istanza 0x1973d10'
Nota: ho accesso al tag TextField (se questo aiuta?)
Sono un po 'oltre il palcoscenico per principianti in Objective-C, ma ancora non eccezionale. Il mio codice è attualmente sotto e a https://gist.github.com/d634329e5ddf52945989
Ringrazia tutti.
MyDetailController.H
@interface MyDetailController : UITableViewController <UITextFieldDelegate,UINavigationControllerDelegate>
{
//...(lots in here)
}
@end
@interface UIResponder(UIResponderInsertTextAdditions)
- (void) insertText: (NSString*) text;
@end
Mydetailcontroller.m
@implementation MyDetailController
//.... (lots in here)
- (void)addDecimal:(NSNotification *)notification {
// Apend the Decimal to the TextField.
//savedAmount.text = [savedAmount.text stringByAppendingString:@"."];
NSLog(@"Decimal Pressed");
NSLog(@"tagClicked: %d",tagClicked);
switch (tagClicked) {
case 7:
//savedAmount.text = [savedAmount.text stringByAppendingString:@"."];
break;
case 8:
//goalAmount.text = [goalAmount.text stringByAppendingString:@"."];
break;
case 9:
//incrementAmount.text = [incrementAmount.text stringByAppendingString:@"."];
break;
case 10:
//incrementAmount.text = [incrementAmount.text stringByAppendingString:@"."];
break;
}
[self insertText:@"."];
}
-(void)textFieldDidBeginEditing:(UITextField *)textfield{
//UITextField *theCell = (UITextField *)sender;
tagClicked = textfield.tag;
NSLog(@"textfield changed. tagClicked: %d",tagClicked);
}
@end
@implementation UIResponder(UIResponderInsertTextAdditions)
- (void) insertText: (NSString*) text
{
// Get a refererence to the system pasteboard because that's
// the only one @selector(paste:) will use.
UIPasteboard* generalPasteboard = [UIPasteboard generalPasteboard];
// Save a copy of the system pasteboard's items
// so we can restore them later.
NSArray* items = [generalPasteboard.items copy];
// Set the contents of the system pasteboard
// to the text we wish to insert.
generalPasteboard.string = text;
// Tell this responder to paste the contents of the
// system pasteboard at the current cursor location.
[self paste: self];
// Restore the system pasteboard to its original items.
generalPasteboard.items = items;
// Free the items array we copied earlier.
[items release];
}
@end
Soluzione
UiviewController è UN UIREPONDOND ... ma non è l'UireSopnd che dovrebbe ricevere il messaggio InsertText. Vuoi chiamare InsertText: su Uitextfield stesso. Se dai un'occhiata a Uiresponder.h vedrai il seguente commento vicino alla pasta: Metodo
// Questi metodi non sono implementati in NSObject
Significa che non è necessariamente sicuro chiamarli su qualsiasi UIRESPONDOND. Possono solo essere chiamati in sicurezza su sottoclassi come Uitextfield e UitextView che le implementano effettivamente. Questa è stata una decisione di design davvero strana da parte di Apple.