本文介绍了< UITextinputDelegate>的问题协议实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的是一个 UITextField ,将字视为字符。具体来说,我试图看到数学表达式(例如,一个字符),我想通过实现我自己的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是一个实例的定制NSObject类采用上述UITextInputDelegate协议
self.textInputDel = [[BIDTextfieldInputDelegate alloc] init];

self.inputDelegate = self.textFieldDel;有没有人知道我做错了什么,还是有更好的解决方案?

解决方案

UITextInputDelegate 不是 相反,系统创建符合 UITextInputDelegate 的对象,并将其分配为符合第一个响应者的 .inputDelegate UITextInput



UITextInputDelegate 的方法是方法为了使您的UITextInput符合回复者在您的 .inputDelegate 上调用通知您已通过除键盘输入之外的方式更改了您的文本或选择。


What I am trying to implement is a UITextField that sees words as characters. Specifically, im trying to see the mathemathical expression sin( as one character for example. I thought to solve this problem by implementing my own UITextInputDelegate. However, the four required functions from this protocol never get called when I implement or adopt this protocol. I tried implementing it in the following ways:

By subclassing a UITextField.

@interface BIDUItextFieldDelegate : UITextField < UITextInputDelegate >

By subclassing a NSObject.

@interface BIDTextfieldInputDelegate : NSObject < UITextInputDelegate >

The corresponding .m file contains:

@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");
}

For example for the second approach (via subclassing NSObject), I do the following in the (id) init method in an additional custom UITextfield class which is displayed within the app:

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

self.inputDelegate = self.textFieldDel;

Does anyone know what I am doing wrong, or has a better solution?

解决方案

UITextInputDelegate is not a protocol that you implement; rather, the system creates an object conforming to UITextInputDelegate, and assigns it as the .inputDelegate of a First Responder that conforms to UITextInput.

The methods of UITextInputDelegate are methods for your UITextInput-conforming responder to call on your .inputDelegate to inform it that you have changed your text or selection via means other than keyboard input.

这篇关于&lt; UITextinputDelegate&gt;的问题协议实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:06