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文字)
  • キーボードのEnterキーを押します
  • プレスequalsPress
  • 編集を押します
  • 表示されるはず:カーソルと空のテキストフィールド
  • 実際に参照してください:最初にカーソルを置いて、最後に入力したものは何でも。
  • 入力すると、このテキストは表示されなくなります。
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

私はあなたの問題の解決策を思いついた、それは良くないが、少なくともそれは機能している: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 に答える
1

コードを複製した後、キーボードのReturnキーを押してから、等しいプレスボタンをタップした場合にのみ問題が発生することがわかりました...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 に答える
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 に答える