的UITextField - 键盘不显示

问题描述:

我有这里面父视图控制器我有自定义视图,当该自定义视图被初始化我安装了另一个自定义视图,该视图有UITextFields,但是当我在的UITextField点击键盘永不显示。的UITextField - 键盘不显示

的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 

自定义视图,包含其他自定义视图,其保持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 

自定义视图,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 

一个原因可能是缺乏UITextField上的明确委托。

设置其给出UITextField实例的代表是父容器(它被添加到一个),这将使代理和的UITextField听更改。然后,你可以进一步推信封,并尽量集中在容器视图初始化的UITextField:

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

然后你可以实现在父视图:

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

最后,代码 - 明智的是,如果LoginSignupViewController不需要用户输入,只需将用户交互设置为NO,并仅在视图中设置需要这样的标志,在本例中为UITextField的容器。

另一个愚蠢的理由,如果你运行你的应用程序在一个模拟器,这可能是一个原因:

“当你的iPhone模拟器是开放的,去(顶部菜单):

硬件>键盘>取消核取somne​​thing像“模拟硬件键盘”

完成了一个!

我希望它能帮助

+0

进出口测试这是我的iPhone设备上 – redoc01 2014-08-28 18:28:59

+0

@ redoc01是您查看控制器被模态呈现的? – Fer 2014-08-28 18:42:01

+0

视图控制器是父视图控制器 – redoc01 2014-08-28 18:43:26

有没有可以合作没有显示键盘的原因。 首先确保,你的UITextField是笔尖互动,并在代码中(的UITextField本身和上海华层次应该是互动)。

接下来的事情你必须做的UITextField的肯定SuperViews足够大,以便保留的UITextField互动。您可以为所有视图提供不同的背景颜色,并查看是否有几个重叠。