objective-c 清除 UITextField 的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1168680/
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
Best way to clear a UITextField
提问by Brenden
For an IBOutlet UITextField, does it matter as far as memory management or other reasons how you clear the text value?
对于IBOutlet UITextField,就内存管理或其他原因而言,您如何清除文本值是否重要?
textFieldX.text = nil
or
或者
textFieldX.text = @"";
In objective-c it is acceptable to message a nilobject and @""is a static NSString *. I'm not sure if every @""points to the same object in memory or if it allocates a bunch of 1 byte null terminated strings.
在 Objective-c 中,可以向nil对象发送消息并且@""是静态的NSString *。我不确定是否每个都@""指向内存中的同一个对象,或者它是否分配了一堆 1 字节的空终止字符串。
Not a big deal, just thought I'd ask the community. Thanks.
没什么大不了的,只是想我会问社区。谢谢。
回答by Brad The App Guy
Personally I would think less of the memory usage here and more on code-maintainability.
就我个人而言,我认为这里的内存使用较少,而更多地考虑代码可维护性。
To me, It makes sense that a label always has a string. In the future someone might try to append a labels value, save it in a database, wrap it in xml, etc. An empty NSString in this case makes much more sense to me that a 0x0.
对我来说,标签总是有一个字符串是有道理的。将来有人可能会尝试附加标签值,将其保存在数据库中,将其包装在 xml 中等。在这种情况下,空 NSString 对我来说比 0x0.0 更有意义。
回答by Dan Lorenc
I usually do textFieldX.text = @"";just for clarity. This helps me remember that the value should be a string, and that I can pass it all the standard string methods.
我通常textFieldX.text = @"";只是为了清楚起见。这有助于我记住该值应该是一个字符串,并且我可以将所有标准字符串方法传递给它。
回答by Denis Kutlubaev
I recommend you to use nil, because I discovered that when you use @"", the placeholder is not aligned in the center(if it must be aligned). Look, how I changed my code to get placeholder aligned properly.
我建议你使用nil,因为我发现当你使用@""时,占位符没有在中心对齐(如果它必须对齐)。看,我是如何更改代码以使占位符正确对齐的。
wordTextField.textAlignment = UITextAlignmentCenter;
//wordTextField.text = @"";
wordTextField.text = nil;
translationTextField.textAlignment = UITextAlignmentCenter;
//translationTextField.text = @"";
translationTextField.text = nil;
回答by TahoeWolverine
Strings always copy in Objective-C, so the second option is most likely creating another string, and then pointing to it. In that way, I think you're right.
字符串总是在 Objective-C 中复制,所以第二个选项很可能是创建另一个字符串,然后指向它。那样的话,我认为你是对的。
To play devil's advocate, I would assume that the compiler optimizes option B to do something like option A anyway. Personally, I would always do option B because it's more readable as far as the end operation you want to accomplish.
为了充当魔鬼的拥护者,我假设编译器优化选项 B 以执行类似于选项 A 的操作。就我个人而言,我总是会选择选项 B,因为就您想要完成的最终操作而言,它更具可读性。
UPDATE: I didn't find a way to accomplish your goal differently, but you may be interested in this tidbit (from Apple UITextField Docs):
更新:我没有找到以不同方式实现目标的方法,但您可能对这个花絮感兴趣(来自Apple UITextField Docs):
clearButtonMode
Controls when the standard clear button appears in the text field.
@property(nonatomic) UITextFieldViewMode clearButtonMode
Discussion
The standard clear button is displayed at the right side of the text field as a way for the user to remove text quickly. This button appears automatically based on the value set for this property.
The default value for this property is UITextFieldViewModeNever.
清除按钮模式
控制标准清除按钮何时出现在文本字段中。
@property(nonatomic) UITextFieldViewMode clearButtonMode
讨论
标准清除按钮显示在文本字段的右侧,作为用户快速删除文本的一种方式。此按钮会根据为此属性设置的值自动出现。
此属性的默认值是 UITextFieldViewModeNever。
I think this would allow you to setup functionality for the user to clear the text field.
我认为这将允许您为用户设置清除文本字段的功能。
回答by Tander
I find that using textField.text = nil;has an undesirable effect on my project. I am building a calculator.
我发现使用textField.text = nil;对我的项目有不良影响。我正在构建一个计算器。
When using the above code, after I press the clear button, pressing a digit does not come up on screen.
使用上述代码时,按下清除按钮后,屏幕上没有出现按数字。
textField.text = @"";
works good for me.
对我有用。

