2

我有一个 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,放置一个UITextField绑定到unitField,并将文本字段的委托设置为文件的所有者。

放置一个UIButton标签equalsPress并将其绑定到IBAction,另一个称为编辑,绑定到setEditingSectionUnits。要查看重现的错误:

  • 运行应用程序
  • 按编辑
  • 在文本字段中输入内容(最少 8-10 个字符)
  • 按键盘上的输入
  • 按等于按
  • 按编辑
  • 应该看到:光标和空文本字段
  • 实际看到:无论您最后键入什么,开头都有一个光标。
  • 键入会使此文本消失。
4

4 回答 4

2

我在复制您的问题时遇到了麻烦我已经模拟了它以使用与您相同的机制并且它按预期工作。

- (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);
    }
  ];
}

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

于 2011-07-17T15:33:23.460 回答
1

复制您的代码后,我发现您提到的问题只有在您按键盘上的回车键然后点击等号按钮时才会出现...在您的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;
    }];
}
于 2011-07-21T13:29:58.733 回答
1

我想出了一个解决你的问题的方法,它不是很好,但至少它可以工作: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;
于 2011-07-19T11:23:21.447 回答
0

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

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

equalsPress 也可以

-(IBAction)equalsPress{
    [unitField setText:@""];
    [UIView animateWithDuration:0.5 animations:^(void){
        [self setEditingSectionValue];
    }];
}
于 2011-07-18T06:39:19.500 回答