2011-10-21 2 views
2

많은 문제가 발생하여 숫자 키패드에 RETURN 버튼을 추가 할 수있었습니다.어떤 uitextfield가 uikeyboard를 호출했는지 감지합니다.

다른 데이터를 다른 uitextfield로 가져올 때 삭제하고 싶습니다. 현재 키보드를 사용할 때마다 해당 버튼이 나타나기 때문에 키보드의 일부입니다.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil]; 

그런 다음 KeyboarWillShow

- (void)keyboardWillShow:(NSNotification *)note { 

    // create custom button 

    NSLog(@"El note es: %@", note); 

    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame = CGRectMake(0, 163, 106, 53); 
    doneButton.adjustsImageWhenHighlighted = NO; 
    [doneButton setImage:[UIImage imageNamed:@"done.jpg"] forState:UIControlStateNormal]; 
    [doneButton setImage:[UIImage imageNamed:@"done_pressed.jpg"] forState:UIControlStateHighlighted]; 
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside]; 

    UIWindow *keyboardWindow = nil; 
    for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) { 
     if (![[testWindow class] isEqual:[UIWindow class]]) { 
      keyboardWindow = testWindow; 
      break; 
     } 
    } 
    if (!keyboardWindow) return; 

    // Locate UIKeyboard. 
    UIView *foundKeyboard = nil; 
    for (UIView *possibleKeyboard in [keyboardWindow subviews]) { 

     // iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView. 
     if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) { 
      possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0]; 
     } 

     if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) { 
      foundKeyboard = possibleKeyboard; 
      break; 
     } 
    } 

    if (foundKeyboard) { 
     // Add the button to foundKeyboard. 
     [foundKeyboard addSubview:doneButton]; 

    } 
} 

에서 것은이 항상 버튼을 생성한다는 것입니다, 그렇지 않으면 일을 나던, KeyBoardDidShow에 이벤트 처리기를 추가! 명백하게! 확실한 uitextfields에서만 버튼을 만들고 싶습니다. 어떻게 그렇게 할 수 있습니까?

메서드 - (BOOL) textFieldShouldBeginEditing : 처음에는 실행하지 않고 나머지는 왜 수행합니까? 모르겠지만, uitextfield가 키보드를 호출하는 것을 알 수 있다면 내 머리카락이 다시 자랄 것이고 더 얇아 질 것입니다. 누구든지 도와 줄 수 있습니까?

답변

0

textFieldShouldBeginEditing:은 텍스트 필드의 각 텍스트 상자가 올바르게 설정된 경우에만 실행됩니다 (XIB를 확인하십시오).

그리고 나서 텍스트 필드에 태그 값을 설정할 수 있습니다. 태그 1 인 필드의 경우 Return 버튼을 표시하고 태그 0 인 필드의 경우 Return 버튼을 표시 할 수 없습니다.

+0

을, 나는 대리자 년대 uitextfields에 대한 고정. 그러나 문제는 textFieldShouldBeginEditing :이 실행될 때 키보드가로드되지 않기 때문에 키보드보기가 빠르기 때문에 반환 버튼을 추가 할 수 없다는 것입니다. keyboardwillshow에서 액세스 할 수 있지만 각 uitextfield의 정보에 액세스 할 수 없습니다. uitextfield가 마지막으로 탭된 속성을 저장하는 속성을 유지해야합니까? – subharb

+0

옙 ... 마지막 UITextField를 저장하는 특성을 유지하는 것이 좋습니다. –

0

키보드의 하위 뷰 수를 계산하여 얼마나 많은 키가 있는지 알 수 있습니다. 즉, 나쁜 해킹,하지만 당신은 이미 또한

다른 제안 :-) 키보드를 찾기 위해 나쁜 해킹이 : 좋아

if (foundKeyboard) { 
    UIView *doneButton = [foundKeyboard viewWithTag:BUTTON_TAG]; 

    if(numPad){ 
     if(buttonView==nil){ 
      //create doneButton 
      [foundKeyboard addSubview:doneButton];   
     } 
    }else{ 
     [doneButton removeFromSuperview]; 
    } 
} 
관련 문제