0

我正在尝试将文本粘贴到光标当前所在的位置。我一直在尝试做它所说的: - http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html

主要问题是我不能只使用 textbox1.text (等),因为文本字段位于自定义单元格的中间。我只想在光标所在的位置添加一些文本(当我按下键盘上的自定义键时)。

-我只想将小数粘贴到文本框中...

我得到的错误是:

2010-05-15 22:37:20.797 PageControl[37962:207] * -[MyDetailController paste:]:无法识别的选择器发送到实例 0x1973d10 2010-05-15 22:37:20.797 PageControl[37962:207] *终止应用程序到期未捕获的异常 'NSInvalidArgumentException',原因:'*** -[MyDetailController paste:]:无法识别的选择器发送到实例 0x1973d10'

注意:我可以访问 textfield 标签(如果有帮助?)

我已经过了objective-c的初学者阶段,但仍然不是很好。我的代码目前在下面,在https://gist.github.com/d634329e5ddf52945989

谢谢大家。


MyDetailController.h

    @interface MyDetailController : UITableViewController <UITextFieldDelegate,UINavigationControllerDelegate>
{

//...(lots in here)

}


@end


@interface UIResponder(UIResponderInsertTextAdditions)
- (void) insertText: (NSString*) text;
@end

我的细节控制器.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
4

1 回答 1

1

UIViewController一个 UIResponder ......但它不是应该接收 insertText: 消息的 UIResopnder。您想在 UITextField 本身上调用 insertText:。如果您查看 UIResponder.h,您会在粘贴附近看到以下注释:方法

// 这些方法在 NSObject 中没有实现

这意味着在任何 UIResponder 上调用它们并不一定安全。它们只能在实际实现它们的 UITextField 和 UITextView 等子类上安全地调用。这对苹果来说是一个非常奇怪的设计决定。

于 2010-05-15T22:30:52.020 回答