下一步按钮不会导航到UITextField

时间:2012-12-15 15:15:40

标签: iphone objective-c ios delegates uitextfield

过了一段时间,在这里搜索一些解决方案我们发现了这篇文章:

How to navigate through textfields (Next / Done Buttons)

我们尝试过但很遗憾它根本不起作用。请注意,我们将UITextFields放在UITableView

我们认为问题在于代表们,但我们不知道如何处理它。以下我向您展示我们的代码:

·H

#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> {

    UITableView *loginTableView;
    UITextField *textField;

}

@property UITableView *loginTableView;
@property UITextField *textField;
@property UIButton *loginButton;
@property UIButton *cancelButton;

@end

的.m:

#import <QuartzCore/QuartzCore.h>
#import "LoginViewController.h"
#import "RootViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

@synthesize loginTableView, textField, loginButton, cancelButton;

- (void)viewDidLoad {

    [super viewDidLoad];

    // Make rounded corners view
    [self.view.layer setCornerRadius:4.0];
    [self.view.layer setMasksToBounds:YES];
    self.view.layer.opaque = NO;
    self.view.backgroundColor = [UIColor whiteColor];

    // Create background image view
    UIImageView *loginBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height+20)];
    // Create background's image
    UIImage *backgroundImage = [UIImage imageNamed:@"loginbackground.png"];
    loginBackgroundImageView.image = backgroundImage;
    [self.view addSubview:loginBackgroundImageView];

    // Create logo's image
    UIImage *logoImage = [UIImage imageNamed:@"logo.png"];

    // Create logo's image view
    UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2 - logoImage.size.width/2, self.view.bounds.size.height/9, logoImage.size.width, logoImage.size.height)];

    // Set image to logo's image view
    logoImageView.image = logoImage;

    [self.view addSubview:logoImageView];

    // Create login table view
    loginTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2 - (self.view.bounds.size.width/1.2)/2, self.view.bounds.size.height/3, self.view.bounds.size.width/1.25, 100) style:UITableViewStylePlain
                      ];
    loginTableView.delegate = self;
    loginTableView.dataSource = self;

    // Create login container table view
    UIImage *containerImage = [UIImage imageNamed:@"loginform.png"];
    UIImageView *containerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2 - containerImage.size.width/2, self.view.bounds.size.height/3, containerImage.size.width, containerImage.size.height)];
    containerImageView.image = containerImage;
    [self.view addSubview:containerImageView];

    // Custom table view
    loginTableView.backgroundColor = [UIColor clearColor];
    loginTableView.separatorColor = [UIColor clearColor];

    // Disable scroll
    loginTableView.scrollEnabled = NO;

    // Add login table view to main view
    [self.view addSubview:loginTableView];

    // Create buttons images
    UIImage *loginImage = [UIImage imageNamed:@"loginbtn2.png"];
    UIImage *cancelImage = [UIImage imageNamed:@"cancelbtn.png"];

    // Create buttons
    loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];

    // Set buttons' normal state image
    [loginButton setImage:loginImage forState:UIControlStateNormal];
    [cancelButton setImage:cancelImage forState:UIControlStateNormal];

    // Place buttons
    loginButton.frame= CGRectMake((self.view.bounds.size.width/2)-(loginImage.size.width/2), self.view.bounds.size.height/1.65, loginImage.size.width, loginImage.size.height);
    cancelButton.frame= CGRectMake((self.view.bounds.size.width/2)-(cancelImage.size.width/2), self.view.bounds.size.height/1.375, cancelImage.size.width, cancelImage.size.height);

    // Set buttons' action
    [loginButton addTarget:self action:@selector(loginButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    // Set button to the main view
    [self.view addSubview:loginButton];
    [self.view addSubview:cancelButton];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 2;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if ([indexPath section] == 0) {
            textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 11, 200, 25)];
            textField.adjustsFontSizeToFitWidth = NO;
            textField.textColor = [UIColor darkGrayColor];
            textField.backgroundColor = [UIColor clearColor];
            if ([indexPath row] == 0) {
                textField.placeholder = @"Username";
                textField.keyboardType = UIKeyboardTypeEmailAddress;
                textField.returnKeyType = UIReturnKeyNext;
                textField.autocorrectionType = UITextAutocorrectionTypeNo;
                textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
                textField.tag = 0;
            }
            else {
                textField.placeholder = @"Password";
                textField.keyboardType = UIKeyboardTypeEmailAddress;
                textField.returnKeyType = UIReturnKeyDone;
                textField.autocorrectionType = UITextAutocorrectionTypeNo;
                textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
                textField.secureTextEntry = YES;
                textField.tag = 1;
            }
            textField.clearButtonMode = UITextFieldViewModeAlways;
            textField.delegate = self;
            [textField setEnabled: YES];
            [cell addSubview:textField];
        }
    }
    if ([indexPath section] == 0) { // Email & Password Section
        if ([indexPath row] == 0) { // Email
            cell.imageView.image = [UIImage imageNamed:@"usernameico.png"];
        }
        else {
            cell.imageView.image = [UIImage imageNamed:@"passwordico.png"];
        }
    }
    else { // Login button section
        cell.textLabel.text = @"Log in";
    }
    return cell;    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *) tableView {
    return 1;
}

-(BOOL)textFieldShouldReturn:(UITextField*) formTextField {
    NSInteger nextTag = formTextField.tag + 1;
    // Try to find next responder
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
    if (nextResponder) {
        // Found next responder, so set it.
        [nextResponder becomeFirstResponder];
    } else {
        // Not found, so remove keyboard.
        [formTextField resignFirstResponder];
    }
    return NO; // We do not want UITextField to insert line-breaks.
}

- (void)loginButtonPressed: (UIButton *) sender {

}

- (void)cancelButtonPressed: (UIButton *) sender {

}

@end

提前谢谢!

3 个答案:

答案 0 :(得分:1)

逐个为textField添加标记并使用textField委托

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    int NextTextField = textField.tag+1;

    UITextField *txt = (UITextField *) [tableView viewWithTag:NextTextField];
    [txt becomeFirstResponder];
    return NO;
}

答案 1 :(得分:1)

问题在于表格。当您询问文本视图的超级视图时,它将返回不包含任何其他兄弟的单元格。尝试:

UIResponder* nextResponder = [tableView viewWithTag:nextTag];

答案 2 :(得分:1)

使用cellForRowAtIndexPath:方法中的TextField创建一个数组..

[txtArray addObject:textField]; 

然后这个波纹管方法会起作用......

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    int i=[txtArray count];
    int x=textField.tag+1;
    UITextField *nextTextField = (UITextField *)[txtArray objectAtIndex:x];
    if (x<i-1) {
        [nextTextField becomeFirstResponder];
    } else {
        [textField resignFirstResponder];
    }
    return YES;
}

我希望这可以帮助你...