2011-08-12 4 views
0

를 클릭 할 때의 UITextField 값을 얻는 방법 :버튼이 나는이보기가

enter image description here

이러한 구현 :

탭이없는 사용자가 클릭 로그인 버튼을 닫기 위해 "완료"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [self.tableLogin dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     if ([indexPath section] == 0) { 
      UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 165, 30)]; 
      textfield.adjustsFontSizeToFitWidth = YES; 
      textfield.textColor = [UIColor blackColor]; 
      textfield.backgroundColor = [UIColor whiteColor]; 
      textfield.autocorrectionType = UITextAutocorrectionTypeNo; 
      textfield.autocapitalizationType = UITextAutocapitalizationTypeNone; 
      textfield.textAlignment = UITextAlignmentLeft; 
      textfield.clearButtonMode = UITextFieldViewModeNever; 
      textfield.delegate = self; 

      if ([indexPath row] == 0) { 
       textfield.tag = 0; 
       textfield.placeholder = @"your username"; 
       textfield.keyboardType = UIKeyboardTypeDefault; 
       textfield.returnKeyType = UIReturnKeyNext; 
      } 
      else { 
       textfield.tag = 1; 
       textfield.placeholder = @"required"; 
       textfield.keyboardType = UIKeyboardTypeDefault; 
       textfield.returnKeyType = UIReturnKeyDone; 
       textfield.secureTextEntry = YES; 
      } 

      [textfield setEnabled:YES]; 
      [cell addSubview:textfield]; 
      [textfield release]; 
     } 
    } 
    if ([indexPath section] == 0) { 
     if ([indexPath row] == 0) { 
      cell.textLabel.text = @"Email"; 
     } 
     else { 
      cell.textLabel.text = @"Password"; 
     } 
    } 

    return cell;  
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    switch (textField.tag) { 
     case 0: 
      self.username = textField.text; 
      break; 
     case 1: 
      self.password = textField.text; 
      break; 
    } 
    return true; 
} 

키보드는 필드 값을 저장하지 않습니다. 어떻게하면이 문제를 해결할 수 있습니까?

답변

2

두 가지 옵션 :

두 텍스트 필드에 대한 참조를 유지하고 "완료"버튼의 액션 메소드에서 자신의 text 값을 당깁니다.

또는 UITextFieldTextDidChangeNotification에 등록하고 입력을 캡처하십시오.

+0

'UITextFieldTextDidChangeNotification'이란 무엇입니까? 나는 그것의 아무것도 찾을 수 없습니다. –

+0

UITextField에 정의 된 알림 이름입니다. '[NSNotificationCenter defaultCenter] addObserver : 셀렉터 : @selector (textFieldDidChange :) name : UITextFieldTextDidChangeNotification object : textField];' –

+1

문서 *가 이상하게 희박합니다. 22 페이지 중 * PDF *에는 클래스에서 지원하는 모든 알림이 나열되어 있습니다. http://developer.apple.com/library/ios/documentation/uikit/reference/UITextField_Class/UITextField_Class.pdf –

1

- (void)textFieldDidEndEditing:(UITextField *)textField을 사용하고 거기에 값을 저장하고 반송 키를 누를 수 있습니다.

+0

무효 대신 IBAction을 사용하기 위해 내 편을 변경했습니다. - (IBAction) textFieldDidEndEditing : (UITextField *) textField 및 "편집 변경됨"이벤트를이 작업으로 참조했습니다. 매력처럼 일했습니다. –