iPhone 应用程序文本字段无法正常工作

标签 iphone

我目前正在开发一个具有 3 个文本字段的 Iphone 应用程序。如果我将前两个文本字段的委托(delegate)连接到类,然后运行模拟器并尝试单击它们,则不会发生任何情况,它们不允许我编辑它们,并且键盘不会弹出。如果我不连接他们的代表,则会出现键盘,但当我单击键盘上的“完成”按钮时,永远不会调用 textFieldShouldReturn。第三个文本字段在单击时会显示一个 UIPickerView,并按预期显示。

LoginViewController.h

#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController     <UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
{
IBOutlet UITextField *usernameField;
IBOutlet UITextField *passwordField;
IBOutlet UITextField *conferenceField;
IBOutlet UIButton *loginButton;
//IBOutlet UIPickerView *picker;
ConnectHandler *cHandle;    
NSMutableArray *conferences;
}

@property (nonatomic, retain) UITextField *usernameField;
@property (nonatomic, retain) UITextField *passwordField;
@property (nonatomic, retain) UITextField *conferenceField;
@property (nonatomic, retain) UIButton *loginButton;

- (IBAction) login: (id) sender;

@end

LoginViewController.m

#import "LoginViewController.h"


@implementation LoginViewController

@synthesize usernameField;
@synthesize passwordField;
@synthesize conferenceField;
@synthesize loginButton;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization.
}
return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
cHandle = [ConnectHandler new];

NSArray *confs = [cHandle conference_list];
//conferences = confs;

// temporary to test if it's working
conferences = [NSArray arrayWithObjects:@"Germany", @"Austria", @"Swiss", @"Luxembourg", 
               @"Spain", @"Netherlands", @"USA", @"Canada", @"Denmark", @"Great Britain",
               @"Finland", @"France", @"Greece", @"Ireland", @"Italy", @"Norway", @"Portugal",
               @"Poland", @"Slovenia", @"Sweden", nil];

UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectZero];
picker.delegate = self;
picker.dataSource = self;
[picker setShowsSelectionIndicator:YES];
[conferenceField setInputView:picker];
[picker release];
[picker selectRow:1 inComponent:0 animated:NO];

}


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

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
[conferences release];
}

- (IBAction) login: (id) sender
{
loginButton.enabled = FALSE;

NSLog(@"user: %@ pass: %@", usernameField.text, passwordField.text);
//checks to see if user provided information is valid
NSString *db = @"sdfsdf";

BOOL auth = [cHandle check_auth:db :usernameField.text :[cHandle hashPass:passwordField.text]]; 
NSLog(@"AUTH: %@", auth?@"YES":@"NO");
// login successful if check_auth returns YES
if (auth == YES) {
    // store the user's login info
    // switch to full app


    [self dismissModalViewControllerAnimated:YES];
}
else {
    // display error message and stay on login screen
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Invalid"
                          message:[NSString stringWithFormat:@"The login or password you have entered is invalid"]
                          delegate:nil 
                          cancelButtonTitle:@"Okay" 
                          otherButtonTitles:nil];
    [alert show];
    [alert release];

    loginButton.enabled = TRUE;
}

//NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
//[pickerView setHidden:NO];
}


//#pragma mark -

//#pragma mark UIPickerViewDelegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSLog(@"titleForRow");
return @"TEST"; //[conferences objectAtIndex:row];
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
NSLog(@"didSelectRow");
[self textFieldShouldReturn:conferenceField];
//  [pickerView resignFirstResponder];
//conferenceField.text = (NSString *)[conferences objectAtIndex:row];

}

//#pragma mark -

//#pragma mark UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
NSLog(@"numberOfComponentsInPickerView");
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSLog(@"numberOfRowsInComponent");
return 4; //[conferences count];
}

@end

最佳答案

将其添加到您的 viewDidLoad 中:

usernameField.delegate = self;
passwordField.delegate = self;
conferenceField.delegate = self;

您没有设置文本字段的委托(delegate)。另请从您的 textFieldDidBeginEditing 中删除 resignFirstResponder 代码,如其他答案中所述。

关于iPhone 应用程序文本字段无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7015021/

相关文章:

javascript - 在 Android 和 iPhone 的 JS 链接标签上触发点击事件

iphone - 如何更改自动生成的导航栏 Xcode

iphone - Canvas 绘图线在 phonegap 中使用 android 非常慢

iphone - 使用 CGContextDrawLinearGradient 时如何优化 UITableViewCell 渲染

iphone - 如何识别 UIPickerView 上的滚轮旋转动画?

iphone - 如何在AVAudioPlayer中按顺序循环播放多个音频文件?

iphone - 访问 iOS 中的设置应用程序

iphone - Wowza:需要将 rtp-live 流式传输到 iPhone

iphone - 将数组从 NSObject 传递到 View Controller

iphone - 为什么我无法在 Interface Builder 中更改 View Controller 的 View 大小?