协议实现的问题

2024-04-13

我想要实现的是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;

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


UITextInputDelegate不是一个协议you实施;相反,系统创建一个符合以下条件的对象UITextInputDelegate,并将其指定为.inputDelegate符合以下条件的急救人员UITextInput.

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

协议实现的问题 的相关文章

随机推荐