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の貼り付け:]:認識されないセレクターがインスタンス0x1973d10に送信されました2010-05-15 22:37:20.797 PageControl [37962:207]*アプリの終了期限キャッチされない例外'NSInvalidArgumentException'、理由:'***-[MyDetailControllerペースト:]:認識されないセレクターがインスタンス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
4

1 に答える 1

1

UIViewControllerUIResponderです...しかし、insertText:メッセージを受信するのはUIResopnderではありません。UITextField自体でinsertText:を呼び出します。UIResponder.hを見ると、貼り付けの近くに次のコメントが表示されます。メソッド

//これらのメソッドはNSObjectに実装されていません

つまり、UIResponderだけで呼び出すのは必ずしも安全ではありません。それらは、実際にそれらを実装するUITextFieldやUITextViewなどのサブクラスでのみ安全に呼び出すことができます。これはApple側の本当に奇妙な設計上の決定でした。

于 2010-05-15T22:30:52.020 に答える