Pregunta

Estoy tratando de pegar texto directamente en donde está actualmente el cursor. He estado tratando de hacer lo que dice en: - http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html

El trato principal es que no puedo ir a TextBox1.text (etc.) porque el campo de texto está en el medio de una celda personalizada. Solo quiero que se agregue algún texto donde está el cursor (cuando presiono una tecla personalizada en un teclado).

-NOTO quiero pegar un decimal en el cuadro de texto ...

El error que recibo es:

2010-05-15 22: 37: 20.797 pagecontrol [37962: 207 *-[MyDetailController Paste:]: Selector no reconocido enviado a la instancia 0x1973d10 2010-05-15 22: 37: 20.797 pagecontrol [37962: 207] * * Aplicación de terminación debido a la excepción no captura 'nsinvalidarGumentException', razón: '*** -[myDetailController Paste:]: selector no reconocido enviado a la instancia 0x1973d10'

Nota: Tengo acceso a la etiqueta TextField (si eso ayuda?)

Estoy un poco más allá de la etapa principiante en Objective-C, pero aún no es genial. Mi código está actualmente a continuación y en https://gist.github.com/d634329e5ddf52945989

Gracias a todos.


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
¿Fue útil?

Solución

UiviewController es un UIRESPONDER ... pero no es el UiresOpnder el que debe recibir el Mensaje InsertText: Mensaje. Desea llamar a InsertText: en el propio UITextfield. Si echas un vistazo a UIiresponder.H verás el siguiente comentario cerca de la pasta: Método

// Estos métodos no se implementan en NSObject

Lo que significa que no es necesariamente seguro llamarlos a cualquier UIIRSponder. Solo pueden llamarse de manera segura a subclases como UITextfield y UITextView que realmente los implementan. Esta fue una decisión de diseño realmente extraña por parte de Apple.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top