2014-08-28 148 views
0

我有這裏面父視圖控制器我有自定義視圖,當該自定義視圖被初始化我安裝了另一個自定義視圖,該視圖有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 

回答

1

一個原因可能是缺乏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

0

有沒有可以合作沒有顯示鍵盤的原因。 首先確保,你的UITextField是筆尖互動,並在代碼中(的UITextField本身和上海華層次應該是互動)。

接下來的事情你必須做的UITextField的肯定SuperViews足夠大,以便保留的UITextField互動。您可以爲所有視圖提供不同的背景顏色,並查看是否有幾個重疊。