怎样才能摆脱“鬼魂”呢?以编程方式清除该字段后出现在 UITextField 中的文本?
我对 UITextField 有一个奇怪的问题 - 我将其值设置为 @"" (或任何其他仍然导致它的值),然后将字段缩小到零。下次我取消缩小它时,它会显示与缩小之前相同的值,无论我是否更改了视图中的文本。在字段中输入会导致故障消失,但看起来很糟糕。
要重现的完整代码:
throwaway_testViewController.h:
#import <UIKit/UIKit.h>
@interface throwaway_testViewController : UIViewController <UITextFieldDelegate>{
UITextField * unitField;
}
@property (nonatomic, assign) IBOutlet UITextField * unitField;
-(IBAction)setEditingSectionUnits;
-(void)setEditingSectionValue;
-(IBAction)equalsPress;
@end
throwaway_testViewController.m
#import "throwaway_testViewController.h"
@implementation throwaway_testViewController
@synthesize unitField;
#pragma mark - Inputs
-(IBAction)equalsPress{
[UIView animateWithDuration:0.5 animations:^(void){
[unitField setText:@""];
[self setEditingSectionValue];
}];
}
#pragma mark Input Control
-(void)setEditingSectionUnits{
[UIView animateWithDuration:0.5 animations:^(void){
CGRect newRect = unitField.frame;
newRect.size.width = 160;
unitField.frame = newRect;
}completion:^(BOOL completed){
completed ? [unitField setNeedsDisplay] : nil;
}];
[unitField becomeFirstResponder];
}
-(void)setEditingSectionValue{
[UIView animateWithDuration:0.5 animations:^(void){
CGRect newRect = unitField.frame;
newRect.size.width = [unitField.text sizeWithFont:unitField.font constrainedToSize:CGSizeMake(80, 250)].width;;
unitField.frame = newRect;
}completion:^(BOOL completed){
completed ? [unitField setNeedsDisplay] : nil;
}];
[unitField resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[self setEditingSectionValue];
return TRUE;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[self setEditingSectionUnits];
}
@end
在 xib
中,放置一个与 unitField
绑定的 UITextField
,并将文本字段的委托设置为文件的所有者。
放置一个标记为 equalsPress
的 UIButton
并将其与该 IBAction
绑定,另一个名为 edit 的按钮与 setEditingSectionUnits
绑定。要查看重现的错误:
- 运行应用程序
- 按编辑
- 在文本字段中输入内容(至少 8-10 个字符)
- 按键盘上的 Enter
- 按 equals
- 按按编辑
- 应该看到:光标和空文本字段
- 实际看到:您最后输入的任何内容,光标位于开头。
- 键入会使该文本消失。
I have a strange problem with a UITextField - I am setting the value of it to @"" (or anything else still causes it) and then shrinking the field with to zero. Next time I unshrink it, it displays the same value it had before I shrunk it, regardless of my having changed the text in the view. Typing in the field causes the glitch to go away, but it looks bad.
Complete code to reproduce:
throwaway_testViewController.h:
#import <UIKit/UIKit.h>
@interface throwaway_testViewController : UIViewController <UITextFieldDelegate>{
UITextField * unitField;
}
@property (nonatomic, assign) IBOutlet UITextField * unitField;
-(IBAction)setEditingSectionUnits;
-(void)setEditingSectionValue;
-(IBAction)equalsPress;
@end
throwaway_testViewController.m
#import "throwaway_testViewController.h"
@implementation throwaway_testViewController
@synthesize unitField;
#pragma mark - Inputs
-(IBAction)equalsPress{
[UIView animateWithDuration:0.5 animations:^(void){
[unitField setText:@""];
[self setEditingSectionValue];
}];
}
#pragma mark Input Control
-(void)setEditingSectionUnits{
[UIView animateWithDuration:0.5 animations:^(void){
CGRect newRect = unitField.frame;
newRect.size.width = 160;
unitField.frame = newRect;
}completion:^(BOOL completed){
completed ? [unitField setNeedsDisplay] : nil;
}];
[unitField becomeFirstResponder];
}
-(void)setEditingSectionValue{
[UIView animateWithDuration:0.5 animations:^(void){
CGRect newRect = unitField.frame;
newRect.size.width = [unitField.text sizeWithFont:unitField.font constrainedToSize:CGSizeMake(80, 250)].width;;
unitField.frame = newRect;
}completion:^(BOOL completed){
completed ? [unitField setNeedsDisplay] : nil;
}];
[unitField resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[self setEditingSectionValue];
return TRUE;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[self setEditingSectionUnits];
}
@end
In the xib
, place a UITextField
tied to unitField
, and set the delegate of the text field to be file's owner.
Place a UIButton
labeled equalsPress
and tie it to that IBAction
, and another called edit, tied to setEditingSectionUnits
. To see the bug reproduced:
- Run the app
- Press edit
- type something into the text field (min 8-10 characters)
- press enter on the keyboard
- press equalsPress
- press edit
- Should see: cursor and empty text field
- Actually see: whatever you typed last, with a cursor at the start.
- Typing makes this text disappear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我无法复制您的问题,我已将其模拟为使用与您相同的机制,并且它按预期工作。
也许你可以提供更多的代码,因为我觉得你可能遗漏了一些重要的东西,或者你可以指出你的实现与上面的不同之处。
I am having trouble replicating your issue I have mocked this up to use the same sort of mechanisms as you and it works as expected.
Perhaps you can give more of your code as I feel you may have omitted something important or you can point out where you implementation differs to the above.
复制您的代码后,我发现只有当您按键盘上的回车键然后点击等于按钮时才会出现您提到的问题...
在
setEditingSectionValue
方法中调用动画块之前放弃第一响应者解决了该问题:after replicating your code, i found that the problem you mentioned occurs only if you press return on the keyboard and then tap the equals press button...
resigning first responder before calling animation block in your
setEditingSectionValue
method resolved the problem:我想出了一个解决你的问题的方法,它不太好,但至少它可以工作:D
我在头文件中声明了 tmp :
I came up with a solution to your problem, its not nice, but at least its working :D
I declared tmp in the header file:
我快速浏览了一下,但你可以尝试保留 UITextField:
也可以使用 equalsPress
I had a quick look but can you try retaining UITextField:
Also equalsPress can be