[英]iphone sdk - problem pasting into current text location
我正在嘗試將文本直接粘貼到光標當前所在的位置。 我一直在嘗試執行以下操作: -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粘貼:]:無法識別的選擇器已發送到實例0x1973d10 2010-05-15 22:37:20.797 PageControl [37962:207] *終止應用程序到期導致未捕獲的異常“ NSInvalidArgumentException”,原因:“ ***-[MyDetailController粘貼:]:無法識別的選擇器已發送到實例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
UIViewController 是一個UIResponder ...,但不是UIResopnder應該接收insertText:消息。 您要在UITextField本身上調用insertText:。 如果您查看UIResponder.h,則會在paste:方法附近看到以下注釋。
//這些方法未在NSObject中實現
這意味着僅在任何UIResponder上調用它們不一定安全。 只能在實際實現它們的子類(如UITextField和UITextView)上安全地調用它們。 對於蘋果公司而言,這是一個非常奇怪的設計決定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.