2010-05-15 49 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