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

时间:2011-08-10 17:18:35

标签: iphone

我目前正在开发一个有3个文本字段的Iphone应用程序。如果我将前两个文本字段的委托连接到类,然后运行模拟器并尝试单击它们没有任何反应,它们不允许我编辑它们并且键盘不会弹出。如果我没有连接他们的代理,那么键盘会出现,但是当我单击键盘上的完成按钮时,永远不会调用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

3 个答案:

答案 0 :(得分:0)

您在委托方法[textField resignFirstResponder]中有- (void)textFieldDidBeginEditing:,这会使键盘过早消失。

答案 1 :(得分:0)

这是你的问题:

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

当代理连接时,当用户触摸文本字段并立即强制文本字段辞退第一响应者状态(即丢失其键盘)时,它正在接收-textFieldDidBeginEditing:消息。让代理人保持联系,移除-resignFirstResponder来电,这样就可以了。

答案 2 :(得分:0)

将此添加到viewDidLoad:

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

您没有为文本字段设置委托。同时从其他答案中提及的resignFirstResponder中删除textFieldDidBeginEditing代码。