2014-03-14 2 views
0

내 앱에서 기기 회전시 리로드 데이터으로 테이블을 다시로드합니다. 회전하기 전에 UITextfield에 포커스가 있고 키보드가 열려 있으면 포커스가 유지되고 키보드가 열린 채로 남아 있습니다. Reloaddata은 "textfieldshouldEndEditing"및 "KeyboardshouldHide"알림을 호출하므로 회전시 텍스트 필드에 포커스가 맞추어지지 않고 키보드도 닫힙니다. 기기 회전 후 다시 초점 UItextfield

내가 다음 코드를 사용하고이를 달성하기

[myTextfield performSelector:@selector(becomeFirstResponder) 
            withObject:nil 
            afterDelay:1.0f]; 

이 잘 작동하고 키보드 장치를 회전 한 후 다시 열리지 만 나는 키보드 숨기기 버튼을 누를 때 문제가 (지금 키보드가 닫혀) 그런 다음 장치를 돌리십시오. 키보드를 닫은 후 장치를 돌리기 전에 키보드가 여전히 잘못 표시됩니다.

누군가가 무엇이 잘못되었거나이를 달성하기 위해해야한다고 제안 할 수 있습니까? 고마워

답변

0

키보드가 보이면 회전하기 전에 키보드를 볼 수 있고 1 초 안에 표시되도록 예약하십시오. 키보드가 숨겨져 있어도 키보드가 나타나도록 예약하는 것이 문제라고 생각합니다. 그런 경우 정말 performSelector: withObject:afterDelay:이 그 일의 적절한 방법이라고 생각하지 않지만 키보드가 performSelector: withObject:afterDelay:

if ([myTextField isFirstResponder]) { 
    [myTextField resignFirstResponder]; 
    [myTextField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:1.]; 
} 

전에 볼 수 있다면 당신은 확인해야합니다. 대신 willRotateToInterfaceOrientation:duration:didRotateFromInterfaceOrientation: 메쏘드를 사용할 수 있습니다. 당신은 textField 또는 기타 firstResponder (에 대한 참조를 저장할 수 있습니다 즉, 여러에게 textField의 또는 textView의가있는 경우 :

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if ([self.myTextFiled isFirstResponder]) { 
     self.toBeFirstResponderAfterInterfaceOrientationChange = self.myTextFiled; 
     [self.myTextFiled resignFirstResponder]; 
    } 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    if (self.toBeFirstResponderAfterInterfaceOrientationChange) { 
     [self.toBeFirstResponderAfterInterfaceOrientationChange becomeFirstResponder]; 
     self.toBeFirstResponderAfterInterfaceOrientationChange = nil; 
    } 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    self.toBeFirstResponderAfterInterfaceOrientationChange = nil; 
} 
0

어떻게 그냥이 같은 BOOL 클래스 변수를 사용하는 방법에 대한 :

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    hasKeyboardOpen = YES; 
} 

// your own method to hide keyboard you would call when user physically hide keyboard 
- (void)dismissKeyboard 
{ 
    [myTextField resignFirstResponder]; 

    hasKeyboardOpen = NO; 
} 

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if(hasKeybaordOpen == YES) 
    { 
     [myTextfield performSelector:@selector(becomeFirstResponder) 
            withObject:nil 
            afterDelay:1.0f]; 
    } 
}