2010-05-15 50 views
0

我試圖將文本粘貼到光標當前所在的位置。我一直在努力做到: - http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.htmliphone sdk - 粘貼到當前文本位置的問題

最主要的是,我不能只是去textbox1.text(等),因爲文本字段是在自定義單元格的中間。我只想將一些文本添加到光標所在的位置(當我按下鍵盤上的自定義鍵時)。

-I只想小數粘貼到文本框...

我得到的錯誤是:

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粘貼:]:無法識別的選擇器發送到實例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 

回答

1

的UIViewController 一個UIResponder ......但它不是應該收到insertText:消息的UIResopnder。你想在UITextField本身上調用insertText:如果您在UIResponder.h看看你會看到貼近以下注釋:方法

//沒有在NSObject的

含義,它並不一定安全,他們只是任何呼籲實施這些方法UIResponder。它們只能在UITextField和UITextView等子類中被安全地調用,並且實際上可以實現它們。這對蘋果來說是一個非常奇怪的設計決定。

+0

我只是嘗試添加一個空的粘貼:方法,並獲得通過上述錯誤。 爲了使inserText實際插入文本,需要在paste:方法中使用什麼? 乾杯 – oberbaum 2010-05-17 15:58:58

+0

你不想調用insertText或paste:在你的UIViewController對象上,你想調用[textField insertText:text] – ragfield 2010-05-17 16:43:18

+0

嗯,我現在已經解決了,上面至少讓我在右邊方向。謝謝。 – oberbaum 2010-05-17 16:59:58