UITextField,即使输入了一些文本,也返回0的长度

时间:2014-08-29 10:36:48

标签: ios

我有一个UITextField,当我输入一些文本并检查输入的文本字段的长度是什么时始终为0.

-(IBAction)signupButtonTap:(id)sender{

        [errorContainer removeFromSuperview];
        errorY = 0;

        CGRect screenRect = [[UIScreen mainScreen] bounds];
        errorContainer = [[UIView alloc] init];
        NSLog(@"%d",self.fnTextField.text.length);
        if(self.fnTextField.text.length == 0){
        [errorContainer addSubview:[self getErrorItem:@"Must Enter First Name"]];
        }
        if(self.lnTextField.text.length == 0){
        [errorContainer addSubview:[self getErrorItem:@"Must Enter Last Name"]];
        }

        errorContainer.frame = CGRectMake(0, 68, screenRect.size.width, errorY);
        [self.view addSubview:errorContainer];

        UITapGestureRecognizer *errorTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(errorConTap:)];
        [errorTap setNumberOfTouchesRequired:1];
        [errorTap setNumberOfTapsRequired:1];
        [errorContainer addGestureRecognizer:errorTap];

        /**UIImageView *errCloseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(280, 10, 20, 20)];
        UIImage *errCloseImage = [UIImage imageNamed:@"closeerror.png"];
        errCloseImageView.image = errCloseImage;
        [errorContainer addSubview:errCloseImageView];**/
    }

EDITED

//
    //  ViewController.h
    //  InstantForum
    //
    //  Created by trikam patel on 28/08/2014.
    //  Copyright (c) 2014 trikam patel. All rights reserved.
    //

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController <UITextFieldDelegate,UIGestureRecognizerDelegate>
    @property BOOL isLogin;
    @property (nonatomic, strong) UITextField *fnTextField;
    @property UITextField *lnTextField;
    @property UITextField *eTextField;
    @property UITextField *pTextField;
    @property UITextField *paTextField;
    @end



    -(void)setupView{
        if(!self.isLogin){
        [self setupTextFields:10 :78 :@"textbox1.png" :20 :78 :@"Enter First Name" :self.fnTextField :false];
        [self setupTextFields:10 :123 :@"textbox1.png" :20 :123 :@"Enter Last Name" :self.lnTextField :false];
        [self setupTextFields:10 :168 :@"textbox1.png" :20 :168 :@"Enter Email" :self.eTextField :false];
        [self setupTextFields:10 :213 :@"textbox1.png" :20 :213 :@"Enter Password" :self.pTextField :true];
        [self setupTextFields:10 :258 :@"textbox1.png" :20 :258 :@"Enter Password again" :self.paTextField :true];

        UIImageView *btnImageView = [[UIImageView alloc] initWithFrame:CGRectMake(103, 313, 114, 35)];
        UIImage *btnImage = [UIImage imageNamed:@"buttonsignup.png"];
        btnImageView.image = btnImage;
        btnImageView.userInteractionEnabled = YES;
        [self.view addSubview:btnImageView];

        UITapGestureRecognizer *signupTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(signupButtonTap:)];
        [signupTap setNumberOfTouchesRequired:1];
        [signupTap setNumberOfTapsRequired:1];
        [btnImageView addGestureRecognizer:signupTap];
        }
    }



    -(void)setupTextFields :(int)imageX :(int)imageY :(NSString *)imageName :(int)textX :(int)textY :(NSString *)text :(UITextField *)textField :(BOOL)isPassword{
        UIImageView *fnImageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, 300, 35)];
        UIImage *fnImage = [UIImage imageNamed:imageName];
        fnImageView.image = fnImage;
        [self.view addSubview:fnImageView];

        textField = [[UITextField alloc] initWithFrame:CGRectMake(textX, textY, 280, 35)];
        textField.placeholder = text;
        textField.delegate = self;
        textField.textColor = [UIColor colorWithRed:105.0f/255 green:121.0f/255 blue:221.0f/255 alpha:1.0f];
        textField.font = [UIFont fontWithName:@"Helvetica" size:14];
        textField.secureTextEntry = isPassword;
        textField.tag = tagCount;
        [self.view addSubview:textField];

        tagCount += 1;
    }

2 个答案:

答案 0 :(得分:3)

为了清楚起见,这是一个例子。在setupTextFields :方法

之外创建文本字段
self.fnTextField = [[UITextField alloc] init];
[self setupTextFields:10 :78 :@"textbox1.png" :20 :78 :@"Enter First Name" :self.fnTextField :false];
...

然后只需在setupTextFields :方法中设置框架,而不是分配和修改它。

textField.frame = CGRectMake(textX, textY, 280, 35);
textField.placeholder = text;
...

答案 1 :(得分:-1)

可能是您没有使用uitextfielddelegate协议,请确保您已使用该协议和

[self.fnTextField setdelegate:self];

我没有50点声望,这就是为什么无法发表评论的原因。