我正在尝试将文本粘贴到光标当前所在的位置。我一直试图做它所说的: - http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html
主要的交易是我不能只去textbox1.text(etc),因为textfield位于自定义单元格的中间。我想在光标所在的位置添加一些文本(当我按下键盘上的自定义键时)。
- 我只想在文本框中粘贴一个小数...
我得到的错误是:
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
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
答案 0 :(得分:1)
UIViewController 是一个UIResponder ...但是UIResopnder不应该接收insertText:消息。您想在UITextField本身上调用insertText :.如果您查看UIResponder.h,您会在粘贴:方法
附近看到以下注释//这些方法未在NSObject中实现
意味着在任何UIResponder上调用它们并不一定安全。它们只能安全地在子类上调用,例如实际实现它们的UITextField和UITextView。对于Apple来说,这是一个非常奇怪的设计决定。