【问题标题】:iphone sdk - problem pasting into current text locationiphone sdk - 粘贴到当前文本位置的问题
【发布时间】:2010-05-15 20:39:57
【问题描述】:

我正在尝试将文本直接粘贴到光标当前所在的位置。我一直在尝试做它所说的: - 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 '

注意:我可以访问文本字段标签(如果有帮助?)

我已经过了 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

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

【问题讨论】:

    标签: iphone sdk uitextfield pasteboard


    【解决方案1】:

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

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

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

    【讨论】:

    • 我刚刚尝试添加一个空的 paste: 方法,这解决了上述错误。 paste: 方法中需要什么才能让 inserText 实际插入文本?干杯
    • 你不想调用 insertText: 或 paste: 在你的 UIViewController 对象上,你想调用 [textField insertText: text]
    • 好吧,我现在已经解决了,以上至少让我找到了正确的方向。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多