Question

I'm trying to paste text right into where the cursor currently is. I have been trying to do what it says at: - http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html

The main deal is that I can't just go textbox1.text (etc) because the textfield is in the middle of a custom cell. I want to just have some text added to where the cursor is (when I press a custom key on a keyboard).

-I just want to paste a decimal into the textbox...

The error I get is:

2010-05-15 22:37:20.797 PageControl[37962:207] * -[MyDetailController paste:]: unrecognized selector sent to instance 0x1973d10 2010-05-15 22:37:20.797 PageControl[37962:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[MyDetailController paste:]: unrecognized selector sent to instance 0x1973d10'

Note: I have access to the textfield tag (if that helps?)

I'm a little past the beginner stage in objective-c, but still not great. My code is currently below, and at https://gist.github.com/d634329e5ddf52945989

Thanks all.


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
Was it helpful?

Solution

UIViewController is a UIResponder... but it's not the UIResopnder that should receive the insertText: message. You want to call insertText: on the UITextField itself. If you take a look at UIResponder.h you'll see the following comment near the paste: method

// these methods are not implemented in NSObject

Meaning that it's not necessarily safe to call them on just any UIResponder. They can only safely be called on subclasses such as UITextField and UITextView which actually implement them. This was a really strange design decision on Apple's part.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top