gpt4 book ai didi

ios - UITextField - 键盘不显示

转载 作者:行者123 更新时间:2023-11-28 21:59:04 26 4
gpt4 key购买 nike

我在里面有一个父 View Controller ,我有自定义 View ,当这个自定义 View 初始化时,我设置了另一个自定义 View ,这个 View 有 UITextFields,但是当我点击 UITextfield 时,键盘永远不会显示。

View Controller :

    //
// 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

自定义 View ,包含另一个包含 UITextFields 的自定义 View

    //
// 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

自定义 View ,登录 View

    //
// 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 上缺少明确的委托(delegate)。

将其 UITextField 实例的委托(delegate)设置为父容器(它被添加到的容器),这将使委托(delegate)和 UITextField 能够监听更改。然后您可以进一步插入信封并尝试在容器 View 初始化时聚焦 UITextField:

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

然后您可以在父 View 中实现以下内容:

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

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

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

“当您的 iOS 模拟器打开时,转到(顶部菜单):

硬件 > 键盘 > 取消选中“模拟硬件键盘”之类的内容

完成!

希望对你有帮助

关于ios - UITextField - 键盘不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25555490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com