我已经使用了论坛上的所有建议,但仍然无法通过单击下一步按钮从一个uitextfield转到另一个。我是否缺少某些东西或有什么地方不合适。任何帮助都会很棒。谢谢。

这是我所有的代码:

//.h file
#import <UIKit/UIKit.h>

@interface IncomeTransactionViewController : UIViewController


@property (strong, nonatomic) IBOutlet UITextField *businessField;
@property (strong, nonatomic) IBOutlet UITextField *memoField;
@property (strong, nonatomic) IBOutlet UITextField *amountField;


@end

//.m file
#import "IncomeTransactionViewController.h"

@interface IncomeTransactionViewController ()

@end

@implementation IncomeTransactionViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

     //Business UITextField
    self.businessField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 280, 40)];

    self.businessField.returnKeyType = UIReturnKeyNext;
    self.businessField.placeholder = @"Business";
    self.businessField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    self.businessField.adjustsFontSizeToFitWidth = TRUE;
    self.businessField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.businessField];

    //Memo UITextField
    self.memoField = [[UITextField alloc] initWithFrame:CGRectMake(20, 350, 280, 40)];

    self.memoField.returnKeyType = UIReturnKeyNext;
    self.memoField.placeholder = @"Memo";
    self.memoField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    self.memoField.adjustsFontSizeToFitWidth = TRUE;
    self.memoField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.memoField];

    //Amount UITextField
    self.amountField = [[UITextField alloc] initWithFrame:CGRectMake(20, 400, 280, 40)];

    self.amountField.returnKeyType = UIReturnKeyDone;
    self.amountField.placeholder = @"Amount";
    self.amountField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    self.amountField.adjustsFontSizeToFitWidth = TRUE;
    self.amountField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.amountField];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.amountField) {
        [textField resignFirstResponder];

    } else if (textField == self.businessField) {
        [self.businessField resignFirstResponder];
        [self.memoField becomeFirstResponder];

    } else if (textField == self.memoField) {
        [self.memoField resignFirstResponder];
        [self.amountField becomeFirstResponder];
    }
    return YES;
}

@end

最佳答案

您必须在 .h 文件中使用<UITextFieldDelegate>

@interface IncomeTransactionViewController : UIViewController  <UITextFieldDelegate>

.m 文件中,您需要像这样分配委托
self.businessField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 280, 40)];
self.businessField.delegate = self; // add this line
self.businessField.returnKeyType = UIReturnKeyNext;
self.businessField.placeholder = @"Business";
self.businessField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.businessField.adjustsFontSizeToFitWidth = TRUE;
self.businessField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.businessField];

//Memo UITextField
self.memoField = [[UITextField alloc] initWithFrame:CGRectMake(20, 350, 280, 40)];
self.memoField.delegate = self; // add this line
self.memoField.returnKeyType = UIReturnKeyNext;
self.memoField.placeholder = @"Memo";
self.memoField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.memoField.adjustsFontSizeToFitWidth = TRUE;
self.memoField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.memoField];

//Amount UITextField
self.amountField = [[UITextField alloc] initWithFrame:CGRectMake(20, 400, 280, 40)];
self.amountField.delegate = self; // add this line
self.amountField.returnKeyType = UIReturnKeyDone;
self.amountField.placeholder = @"Amount";
self.amountField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.amountField.adjustsFontSizeToFitWidth = TRUE;
self.amountField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.amountField];

这应该做。让我知道它是否仍然不起作用

关于ios - UITextField-下一步按钮(2),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26051648/

10-13 04:05