2011-08-10 36 views
3

我目前正在研究具有3個文本字段的Iphone應用程序。如果我將前兩個文本字段的委託連接到類,然後運行模擬器並嘗試點擊它們,什麼也沒有發生,他們不允許我編輯它們,鍵盤不會彈出。如果我不連接它們的代表,那麼鍵盤出現,但是當我單擊鍵盤上的完成按鈕時,textFieldShouldReturn永遠不會被調用。第三個文本字段在點擊時彈出一個UIPickerView,並按預期顯示。iphone應用程序文本字段無法正常工作

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: %@", [email protected]"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 

回答

0

添加到您的viewDidLoad:

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

你不是設置文本字段的委託。正如其他答案中提到的那樣,從您的textFieldDidBeginEditing中刪除resignFirstResponder代碼。

+0

工作正常!非常感謝你。 –

+0

當然可以!很高興爲您提供幫助(*:*提出您的問題以便真正解釋問題* –

+0

如果您覺得如此,請選擇我的答案爲正確(: )再次,很高興提供幫助。設置代表是很多人忘記的。 –

0

您的委託方法- (void)textFieldDidBeginEditing:這將使鍵盤消失過早有[textField resignFirstResponder]

0

這是你的問題:

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

隨着勾搭上了,當用戶觸摸文本字段它的接收-textFieldDidBeginEditing:消息,並立即迫使文本字段辭職第一響應狀態的代表(即失去其鍵盤)。保持代表連接,刪除-resignFirstResponder電話,它會工作。

相關問題