怎样才能摆脱“鬼魂”呢?以编程方式清除该字段后出现在 UITextField 中的文本?

发布于 2024-11-16 04:05:16 字数 2440 浏览 4 评论 0原文

我对 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,并将文本字段的委托设置为文件的所有者。

放置一个标记为 equalsPressUIButton 并将其与该 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.

Image showing the problem

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

执手闯天涯 2024-11-23 04:05:16

我无法复制您的问题,我已将其模拟为使用与您相同的机制,并且它按预期工作。

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self.unitField setText:@"Testing"];
}

- (IBAction)hideButtonTapped:(id)sender
{
  [UIView animateWithDuration:0.5 animations:^(void)
    {
      [self.unitField setText:@""];
      self.unitField.frame = CGRectMake(10, 10, 0, 30);
    }
  ];
}

- (IBAction)showButtonTapped:(id)sender
{
  [UIView animateWithDuration:0.5 animations:^(void)
    {
      self.unitField.frame = CGRectMake(10, 10, 100, 30);
    }
  ];
}

也许你可以提供更多的代码,因为我觉得你可能遗漏了一些重要的东西,或者你可以指出你的实现与上面的不同之处。

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.

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self.unitField setText:@"Testing"];
}

- (IBAction)hideButtonTapped:(id)sender
{
  [UIView animateWithDuration:0.5 animations:^(void)
    {
      [self.unitField setText:@""];
      self.unitField.frame = CGRectMake(10, 10, 0, 30);
    }
  ];
}

- (IBAction)showButtonTapped:(id)sender
{
  [UIView animateWithDuration:0.5 animations:^(void)
    {
      self.unitField.frame = CGRectMake(10, 10, 100, 30);
    }
  ];
}

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.

故人爱我别走 2024-11-23 04:05:16

复制您的代码后,我发现只有当您按键盘上的回车键然后点击等于按钮时才会出现您提到的问题...
setEditingSectionValue 方法中调用动画块之前放弃第一响应者解决了该问题:

-(void)setEditingSectionValue{
    [unitField resignFirstResponder];
    [UIView animateWithDuration:0.5 animations:^(void){
        CGRect newRect = unitField.frame;
        newRect.size.width = [unitField.text sizeWithFont:unitField.font constrainedToSize:CGSizeMake(80, 500)].width;;
        unitField.frame = newRect;
    }completion:^(BOOL completed){
        completed ? [unitField setNeedsDisplay] : nil;
    }];
}

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:

-(void)setEditingSectionValue{
    [unitField resignFirstResponder];
    [UIView animateWithDuration:0.5 animations:^(void){
        CGRect newRect = unitField.frame;
        newRect.size.width = [unitField.text sizeWithFont:unitField.font constrainedToSize:CGSizeMake(80, 500)].width;;
        unitField.frame = newRect;
    }completion:^(BOOL completed){
        completed ? [unitField setNeedsDisplay] : nil;
    }];
}
夏九 2024-11-23 04:05:16

我想出了一个解决你的问题的方法,它不太好,但至少它可以工作:D

-(IBAction)equalsPress{
      tmp = nil;
      //same code as yours
}

-(void)setEditingSectionUnits{  
    if (tmp != nil) {
        unitField.text = tmp;
    }
    //same code as yours
}

-(void)setEditingSectionValue{
    if ([unitField.text length] > 9) {
            tmp = unitField.text;
            unitField.text = [unitField.text substringToIndex:9];
            [tmp retain];
    }else {
           tmp = nil;
    }
    //same code as yours
}

我在头文件中声明了 tmp :

NSString *tmp;

I came up with a solution to your problem, its not nice, but at least its working :D

-(IBAction)equalsPress{
      tmp = nil;
      //same code as yours
}

-(void)setEditingSectionUnits{  
    if (tmp != nil) {
        unitField.text = tmp;
    }
    //same code as yours
}

-(void)setEditingSectionValue{
    if ([unitField.text length] > 9) {
            tmp = unitField.text;
            unitField.text = [unitField.text substringToIndex:9];
            [tmp retain];
    }else {
           tmp = nil;
    }
    //same code as yours
}

I declared tmp in the header file:

NSString *tmp;
谁把谁当真 2024-11-23 04:05:16

我快速浏览了一下,但你可以尝试保留 UITextField:

@property (nonatomic, retain) IBOutlet UITextField * unitField;

也可以使用 equalsPress

-(IBAction)equalsPress{
    [unitField setText:@""];
    [UIView animateWithDuration:0.5 animations:^(void){
        [self setEditingSectionValue];
    }];
}

I had a quick look but can you try retaining UITextField:

@property (nonatomic, retain) IBOutlet UITextField * unitField;

Also equalsPress can be

-(IBAction)equalsPress{
    [unitField setText:@""];
    [UIView animateWithDuration:0.5 animations:^(void){
        [self setEditingSectionValue];
    }];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文