我有一个父视图控制器,我有自定义视图,当这个自定义视图初始化时,我设置了另一个自定义视图,这个视图有UITextFields,但是当我点击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
答案 0 :(得分:2)
一个原因可能是UITextField上没有明确的委托。
将其UITextField实例的委托设置为父容器(添加到其中的容器),这将使委托和UITextField能够侦听更改。然后你可以进一步推动信封,并在初始化容器视图时尝试聚焦UITextField:
self.tfText.delegate = self; // too many selfs for clarity purpose
然后您可以在父视图中实现以下内容:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
最后,代码方面,如果LoginSignupViewController不需要用户输入,只需将用户交互设置为NO,并且只在视图中设置需要这样的标志,在这种情况下是UITextField的容器。
另一个愚蠢的原因,如果您在模拟器中运行您的应用程序,这可能是一个原因:
“当你的iOS模拟器打开时,转到(顶层菜单):
硬件>键盘>取消选中“模拟硬件键盘”之类的东西
完成!
我希望它有所帮助
答案 1 :(得分:0)
键盘没有显示可能有几个原因。 首先要确保,你的UITextField在nib和代码中是交互式的。(UITextField本身和superview层次结构应该是交互式的。)
下一步你需要确保UITextfield的SuperViews足够大,以便UITextfield保持交互。您可以为所有视图指定不同的背景颜色,并查看是否有少量重叠。