UITextFieldShouldReturn不适用于我的代码

时间:2015-10-05 06:59:45

标签: ios objective-c xcode keyboard uitextfield

我搜索了整个互联网并尝试了不同的方法来实现UITextFieldShouldReturn,但是当我在模拟器中运行它时它就无法工作了。

当用户点击键盘上的下一个按钮时,我要做的是从第一个文本字段(emailTextField)移动到第二个文本字段(nameTextField)。第二个文本字段的键盘相同,当用户在最后一个文本字段(numberTextField)上单击完成按钮时关闭键盘。

这是我的代码,任何人都可以帮我这个吗?

#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>

@end

@implementation ViewController 

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.emailTextField.placeholder = @"Your Pitt Email(optional)";
self.nameTextField.placeholder = @"Lost ID Name";
self.numberTextField.placeholder = @"Lost ID Series Number(optional)";

[self.emailTextField.delegate  self];
[self.nameTextField.delegate self];
[self.numberTextField.delegate  self];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}

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

//release delegate
-(void)dealloc{
self.emailTextField.delegate = nil;
self.nameTextField.delegate = nil;
self.numberTextField.delegate = nil;
}

//dismiss keyboard when it's called
-(void)dismissKeyboard{
[self.emailTextField resignFirstResponder];
[self.nameTextField resignFirstResponder];
[self.numberTextField resignFirstResponder];
}

//when a textfield begins editing, this will happen
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField == self.emailTextField){
    textField.placeholder = @"";
}else if(textField == self.nameTextField){
    textField.placeholder = @"";
}else{
    textField.placeholder = @"";
}
}

//when a textfield ends editing, this will happen
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField == self.emailTextField){
    textField.placeholder = @"Your Pitt Email(optional)";
    [self textFieldShouldReturn:self.emailTextField];
}else if(textField == self.nameTextField){
    textField.placeholder = @"Lost ID Name";
    [self textFieldShouldReturn:self.nameTextField];
}else{
    textField.placeholder = @"Lost ID Series Number(Optional)";
    [self textFieldShouldReturn:self.numberTextField];
}
}

//return button set up
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}

//when confirm is pressed
- (IBAction)confirmButton:(id)sender {
[self dismissKeyboard];
}
@end`

This is my UI

6 个答案:

答案 0 :(得分:1)

试试这段代码。我在这里只拿两个 UITextFields。

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

    if (textField==_txtEmail)
    {
        [_txtEmail resignFirstResponder];
        [_txtPassword becomeFirstResponder];
    }
    else if (textField==_txtPassword)
    {
        [_txtPassword resignFirstResponder];
    }
    return YES;
}

答案 1 :(得分:1)

更改

[self.emailTextField.delegate  self];

self.emailTextField.delegate = self;

还从dealloc中删除这些代理。您可以从viewDidDisappear

中添加它们

答案 2 :(得分:0)

首先,您需要将委托设置为正确。然后,您需要为您的下一个文本字段提供becomeNextResponder调用,该调用最好在shouldReturn委托方法中完成。 在您的viewDidLoad方法中,粘贴此内容。

self.emailTextField.tag = 2000;
self.nameTextField.tag = 3000;
self.numberTextField.tag = 4000;

现在就像这样编辑textFieldShouldReturn方法:

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
if(textfield.tag<4000)
{
   UITextField *nextTextField = (UITextField *)[self.view viewWithTag:textField.tag+1000];
[nextTextField becomeFirstResponder];
}
return YES;
}

答案 3 :(得分:0)

此功能足以从文本字段返回键盘。

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}

你的这3行不起作用。

[self.emailTextField.delegate  self];
[self.nameTextField.delegate self];
[self.numberTextField.delegate  self];

如果您使用的是故事板,请将文本字段的代表与视图相关联。

答案 4 :(得分:0)

你可以使用这段代码。它对我有用..

-(void)tool{

UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
[toolbar sizeToFit];

UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyboard)];
[doneButton setTintColor:[UIColor whiteColor]];

UIBarButtonItem *Next = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(Nextbutton)];
[Next setTintColor:[UIColor whiteColor]];

NSArray *itemsArray = [NSArray arrayWithObjects:Next,flexButton,doneButton,nil];
[toolbar setItems:itemsArray];


[FirstNameTextField setInputAccessoryView:toolbar];
[LastNameTextfield setInputAccessoryView:toolbar];
[EmailAddressTextField setInputAccessoryView:toolbar];
[PasswordTextfield setInputAccessoryView:toolbar];
[ConfirmPasswordTextfield setInputAccessoryView:toolbar];}

-(void)Nextbutton{
if ([FirstNameTextField isFirstResponder])
{
    [LastNameTextfield becomeFirstResponder];
}
else if ([LastNameTextfield isFirstResponder])
{
    [EmailAddressTextField becomeFirstResponder];
}
else if ([EmailAddressTextField isFirstResponder])
{
    [PasswordTextfield becomeFirstResponder];
}
else if ([PasswordTextfield isFirstResponder])
{
    [ConfirmPasswordTextfield becomeFirstResponder];
}
else if ([ConfirmPasswordTextfield isFirstResponder])
{
    [ConfirmPasswordTextfield resignFirstResponder];
}}

-(void)resignKeyboard{
[FirstNameTextField resignFirstResponder];
[EmailAddressTextField resignFirstResponder];
[LastNameTextfield resignFirstResponder];
[PasswordTextfield resignFirstResponder];
[ConfirmPasswordTextfield resignFirstResponder];}

答案 5 :(得分:-1)

你可以随时使用图书馆来完成你需要做的事情! 使用此库:tpAvoidKeyboard

阅读如何使用它并在那里你去所有下一个按钮并在文本字段外点击以关闭键盘和最后一个文本字段上的完成按钮以关闭键盘,所有这些功能已经实现