redoc01
redoc01

Reputation: 2297

UITextField - Keyboard not showing

I have a Parent View Controller inside this i have Custom View, when this Custom View is initialised i setup up another Custom View and this view has the UITextFields, but when i click on the UITextfield the keyboard never shows.

ViewController:

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

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController
    @synthesize loginSignupControlView;

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        self.view.userInteractionEnabled = YES;
        CGRect screenRect = [[UIScreen mainScreen] applicationFrame];

        self.loginSignupControlView = [[LoginSignupControlView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 68)];
        [self.view addSubview:self.loginSignupControlView];

        // Do any additional setup after loading the view, typically from a nib.
    }


    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    @end

Custom View, that contains another Custom View which holds the UITextFields

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

    #import "LoginSignupControlView.h"
    #import "UIHelper.h"
    #import "TextField.h"

    @implementation LoginSignupControlView

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
        [self createView];
        }
        return self;
    }

    -(void)createView{

        self.isLogin = true;
        self.userInteractionEnabled = YES;

        self.backgroundColor = [UIColor colorWithRed:56.f/255 green:56.f/255 blue:57.f/255 alpha:1.0f];

        self.logoLabel = [UIHelper getUILabel:CGRectMake(10,35, 200, 14) :@"INSTANT FORUM" :[UIColor colorWithRed:222.0f/255 green:221.0f/255 blue:221.0f/255 alpha:1.0f] :[UIFont fontWithName:@"Helvetica" size:14]];
        [self addSubview:self.logoLabel];

        self.loginLabel = [UIHelper getUILabel:CGRectMake(190,35, 50, 14) :@"LOGIN" :[UIColor colorWithRed:106.0f/255 green:196.0f/255 blue:229.0f/255 alpha:1.0f] :[UIFont fontWithName:@"Helvetica" size:14]];
        [self addSubview:self.loginLabel];

        self.signupLabel = [UIHelper getUILabel:CGRectMake(250,35, 60, 14) :@"SIGNUP" :[UIColor colorWithRed:106.0f/255 green:196.0f/255 blue:229.0f/255 alpha:1.0f] :[UIFont fontWithName:@"Helvetica" size:14]];
        [self addSubview:self.signupLabel];

        [self setupView];

    }

    -(void)setupView{

        if(self.isLogin){

        CGRect screenRect = [[UIScreen mainScreen] applicationFrame];

        self.loginView = [[LoginView alloc] initWithFrame:CGRectMake(0, 68, screenRect.size.width, 286)];
        [self addSubview:self.loginView];

        self.imageViewFN = [UIHelper getUIImageView:CGRectMake(10, 10, 300, 35) :@"textbox1.png"];
        [self.loginView addSubview:self.imageViewFN];

        TextField *fnText = [[TextField alloc] initWithFrame:CGRectMake(20, 10, 280, 35)];
        //fnText.backgroundColor = [UIColor clearColor];
        fnText.text = @"Enter First Name";
        fnText.userInteractionEnabled = YES;
        fnText.enabled = YES;
        fnText.font = [UIFont fontWithName:@"Helvetica" size:14];
        fnText.textColor = [UIColor colorWithRed:105.0f/255 green:121.0f/255 blue:221.0f/255 alpha:1.0f];
        [self.loginView addSubview:fnText];

        }


    }



    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    }
    */

    @end

Custom View, LoginView

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

    #import "LoginView.h"

    @implementation LoginView

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {

        self.backgroundColor = [UIColor colorWithRed:61.0f/255 green:81.0f/255 blue:168.0f/255 alpha:1.0f];
        self.userInteractionEnabled = YES;


        }
        return self;
    }

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    }
    */

    @end

Upvotes: 0

Views: 262

Answers (2)

Fer
Fer

Reputation: 460

One reason could be the lack of a clear delegate on the UITextField.

Set its delegate of the UITextField instance to be the parent container (the one it is added to), that would enable the delegate and the UITextField to listen for changes. Then you could further push the envelop and try to focus the UITextField when the container view is initialized:

self.tfText.delegate = self; // too many selfs for clarity purpose

And then you could implement the following in the parent view:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 
    return YES;
}

Finally, code-wise, if LoginSignupViewController doesnt need user input, just set user interaction to NO and only set the flag in the view which would require such, in this case the container of the UITextField.

Another silly reason, IF you are running your app in a Simulator this MIGHT be a reason:

"when your iOS Simulator is open, go to (top menu):

Hardware > Keyboard > uncheck the one that says somnething like "Simulate Hardware Keyboard"

Done!

I hope it helps

Upvotes: 2

Haris
Haris

Reputation: 1862

There can be couple of reasons why keyboard is not showing. First make sure, your UITextField is interactive in nib and also in code.(UITextField itself and in superview hierarchy should be interactive).

Next Thing you have to make sure SuperViews of UITextfield are big enough so UITextfield remain interactive. You can give different background colors to all your views and see if few are overlapping.

Upvotes: 0

Related Questions