我正在尝试将文本粘贴到光标当前所在的位置。我一直在尝试做它所说的: - 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'
注意:我可以访问 textfield 标签(如果有帮助?)
我已经过了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
我的细节控制器.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