【问题标题】:Problems with <UITextinputDelegate> protocol implementation<UITextinputDelegate> 协议实现的问题
【发布时间】:2013-08-14 22:24:48
【问题描述】:

我想要实现的是UITextField,它将单词视为字符。具体来说,我试图将数学表达式 sin( 例如作为一个字符。我想通过实现我自己的 UITextInputDelegate 来解决这个问题。但是,当我实现或采用这个协议时,这个协议中的四个必需函数永远不会被调用。我尝试通过以下方式实现它:

通过继承 UITextField

@interface BIDUItextFieldDelegate : UITextField < UITextInputDelegate >

通过继承一个 NSObject。

@interface BIDTextfieldInputDelegate : NSObject < UITextInputDelegate >

对应的.m文件包含:

@implementation BIDTextfieldInputDelegate


- (void)selectionWillChange:(id <UITextInput>)textInput
{
    NSLog(@"sWill");
}

- (void)selectionDidChange:(id <UITextInput>)textInput
{
    NSLog(@"sDid");
}

- (void)textWillChange:(id <UITextInput>)textInput
{
    NSLog(@"tWill");
}

- (void)textDidChange:(id <UITextInput>)textInput
{
    NSLog(@"tDid");
}

例如对于第二种方法(通过子类化 NSObject),我在应用程序中显示的附加自定义 UITextfield 类的 (id) init 方法中执行以下操作:

//textInputDel is an instance of the custom NSObject class that adopts the above UITextInputDelegate protocol
self.textInputDel = [[BIDTextfieldInputDelegate alloc] init];

self.inputDelegate = self.textFieldDel;

有谁知道我做错了什么,或者有更好的解决方案?

【问题讨论】:

  • apptree.net/parser.htm 可以帮忙。
  • 感谢您的快速评论,我正在使用 DDmathparser。解析本身一切正常,但例如能够同时删除几个字符或在固定单词中限制插入可能性是我目前正在处理的更多问题。
  • 您可以找到解决问题的方法here

标签: ios objective-c delegates uitextinput


【解决方案1】:

UITextInputDelegate 不是实现的协议;相反,系统会创建一个符合UITextInputDelegate 的对象,并将其分配为符合UITextInput 的First Responder 的.inputDelegate

UITextInputDelegate 的方法是符合 UITextInput 的响应者调用.inputDelegate 的方法,以通知它您已通过键盘输入以外的方式更改文本或选择。

【讨论】:

  • NM,我看错了这篇文章的日期,并认为它正在讨论使用它的键盘扩展问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 2012-03-08
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多