ios 如何在Objective-C中设置文本字段的委托?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11837188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:37:31  来源:igfitidea点击:

How to set delegate of text field in Objective-C?

objective-ciosdelegatesuitextfield

提问by Kyle Clegg

I'd like to customize a UITextFieldby limiting it to four chars. I'm trying to understand how delegates work in Objective-Cand have gone through the following steps to implement this functionality, still with no luck getting a working solution.

我想UITextField通过将其限制为四个字符来自定义 a 。我试图了解委托是如何工作的,Objective-C并且已经通过以下步骤来实现此功能,但仍然没有找到可行的解决方案。

1) Created a LimitedLengthTextField objective-c class. Made the class of type UITextFieldand accept objects of type < UITextFieldDelegate >.

1) 创建了一个 LimitedLengthTextField 目标 C 类。创建类型的类UITextField并接受 < UITextFieldDelegate > 类型的对象。

LimitedLengthTextField.h:

LimitedLengthTextField.h:

@interface LimitedLengthTextField : UITextField <UITextFieldDelegate>
@end

2) Implemented the following method in LimitedLengthTextField.m:

2) 在 LimitedLengthTextField.m 中实现了以下方法:

@implementation LimitedLengthTextField

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 4) ? NO : YES;
}

@end

3) Imported "LimitedLengthTextField.h" in my CreateAccount class and tried to set the delegate of the UITextField "ssnTextField" in viewDidLoadas follows (my app accepts the last 4 digits of the user's SSN).

3) 在我的 CreateAccount 类中导入“LimitedLengthTextField.h”,并尝试如下设置 UITextField“ssnTextField”的委托viewDidLoad(我的应用程序接受用户 SSN 的最后 4 位数字)。

// Set the custom SSN textfield delegate
LimitedLengthTextField *custTextField = [[LimitedLengthTextField alloc] init];
[self.ssnTextField setDelegate:custTextField];

Based on my limited understanding of Objective-Cand delegates, I've now created a class, implemented the delegate method I want, then created an instance of that class and assigned it to my UITextView object. What am I missing?

基于我对Objective-C委托的有限理解,我现在创建了一个类,实现了我想要的委托方法,然后创建了该类的一个实例并将其分配给我的 UITextView 对象。我错过了什么?

回答by user523234

You should not have subclassed UITextField. ?Instead you implement the callbacks in your CreateAccount class. ? So you should have something like this:

您不应该继承 UITextField。? 而是在 CreateAccount 类中实现回调。? 所以你应该有这样的东西:

@interface CreateAccount :UIViewController <UITextFieldDelegate>
// I use UIViewController but whatever your CreateAccount from.

And implement this in your?CreateAccount.m file:

并在您的?CreateAccount.m 文件中实现:

This is probably in your viewDidLoad method:

这可能在您的 viewDidLoad 方法中:

Self.cusTextField.delegate = self;

and this

和这个

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
? ? NSUInteger newLength = [textField.text length] + [string length] - range.length;
? ? return (newLength > 4) ? NO : YES;
}

回答by Selkie

Maybe you should make the CreateAccount (I assume it is a view controller that contains the text field?) conforms UITextFieldDelegateinstead of custom text field, and implement shouldChangeCharactersInRangemethod also in CreateAccount class. Besides, change the delegate to

也许你应该让 CreateAccount(我假设它是一个包含文本字段的视图控制器?)符合UITextFieldDelegate而不是自定义文本字段,并shouldChangeCharactersInRange在 CreateAccount 类中实现方法。此外,将委托更改为

_ssn.delegate = self;

In this case, maybe you don't need a custom text field at all.

在这种情况下,您可能根本不需要自定义文本字段。

回答by bdev

The textfield that should implement that method should be of type LimitedLengthTextField. Just have the others be of type UITextField

应该实现该方法的文本字段应该是 LimitedLengthTextField 类型。只是让其他人是 UITextField 类型

回答by borrrden

You are setting your delegate to a local object which I assume gets deallocated at the end of the current scope. You are responsible for making sure the delegate lives at least as long as its corresponding object. A better way is to implement the delegate method in your view controller and set your custom field's delegate to that. It will only affect that one, not the others. Another way is to implement the delegate internally with a private class. Then you only need to set the class and be done.

您将您的委托设置为一个本地对象,我认为该对象在当前范围的末尾被解除分配。您有责任确保委托的生活至少只要其对应的对象。更好的方法是在视图控制器中实现委托方法并将自定义字段的委托设置为该方法。它只会影响那个,而不是其他。另一种方法是使用私有类在内部实现委托。然后你只需要设置类并完成。