我试图将文本粘贴到光标当前所在的位置。我一直在努力做到: - 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
我只是尝试添加一个空的粘贴:方法,并获得通过上述错误。 为了使inserText实际插入文本,需要在paste:方法中使用什么? 干杯 – oberbaum 2010-05-17 15:58:58
你不想调用insertText或paste:在你的UIViewController对象上,你想调用[textField insertText:text] – ragfield 2010-05-17 16:43:18
嗯,我现在已经解决了,上面至少让我在右边方向。谢谢。 – oberbaum 2010-05-17 16:59:58