1

しばらくして、ここでいくつかの解決策を検索すると、次の投稿が見つかりました。

テキストフィールドをナビゲートする方法 (次へ / 完了ボタン)

試してみましたが、残念ながらまったく機能しません。UITextFields中に aを入れることに注意してください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

前もって感謝します!

4

3 に答える 3

1

textField にタグを 1 つずつ付与し、textField デリゲートを使用します

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

    UITextField *txt = (UITextField *) [tableView viewWithTag:NextTextField];
    [txt becomeFirstResponder];
    return NO;
}
于 2012-12-15T15:56:39.500 に答える
1

問題はテーブルです。スーパービューのテキストビューを要求すると、他の兄弟を含まないセルが返されます。試す:

UIResponder* nextResponder = [tableView viewWithTag:nextTag];
于 2012-12-15T21:18:35.363 に答える
1

cellForRowAtIndexPath:メソッド内で UITextField を使用して配列を作成します。

[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;
}

これがお役に立てば幸いです...

于 2012-12-19T06:38:28.800 に答える