gpt4 book ai didi

iphone - 下一个按钮不浏览 UITextField

转载 作者:行者123 更新时间:2023-12-01 19:15:03 25 4
gpt4 key购买 nike

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

How to navigate through textfields (Next / Done Buttons)

我们试过了,但不幸的是它根本不起作用。请注意,我们将 UITextFieldsUITableView 内.

我们认为问题出在委托(delegate)身上,但我们不知道如何处理。下面我向您展示我们的代码:

.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

先感谢您!

最佳答案

问题是 table 。当您向 TextView 询问它的 super View 时,它将返回不包含任何其他同级的单元格。尝试:

UIResponder* nextResponder = [tableView viewWithTag:nextTag];

关于iphone - 下一个按钮不浏览 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13893394/

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