2011-08-10 10 views
0

numberkeyboard를 사용 중이며 bottem 왼쪽 구석에 완료 키를 추가하고 있습니다. 키보드에서이 버튼을 제거하고 싶습니다. 나중에 일반 키보드를 사용하기 때문에 완료 버튼이 계속 표시됩니다. 전 세계적으로 UIButton을 선언하고 그것에 removeFromSuperview를 호출하고 있습니다. 아무 반응이 없습니다. 또한 removeFromSuperview 메서드를 추가 할 때 removeFromSuperview 메서드를 호출하면 성공적으로 단추가 제거됩니다. 여기에 몇 가지 코드가 있습니다.키보드에서 UIButton을 제거 할 수 없습니다.

-(void)addHideKeyboardButtonToKeyboard{ 


// Locate non-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) { 
    // create custom button 
    NSString *deviceType = [UIDevice currentDevice].model; 
    if(!([deviceType isEqualToString:@"iPad"] || [deviceType isEqualToString:@"iPad Simulator" ])) 
    { 
     doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     doneButton.frame = CGRectMake(0, 163, 106, 53); 
     doneButton.adjustsImageWhenHighlighted = NO; 
     [doneButton setImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal]; 
     [doneButton setImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted]; 
     [doneButton addTarget:self action:@selector(textFieldShouldReturn:) forControlEvents:UIControlEventTouchUpInside]; 
     [foundKeyboard addSubview:doneButton]; 
    } 
} 
} 

과 doneButton을 제거하는

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
    { 
     [doneButton removeFromSuperView]; 
    } 

답변

0

실행 가능한 옵션은 다음, 전무로 doneButton을 설정 realloc 함수와이 ... 결함 removeFromSuperview 것만 큼 쉬운 일이 아닙니다 필요한 때마다 초기화하는 것입니다 : 그럼에도 불구하고 옵션이 있습니다. 내가 제안한 방법이 메모리 누수를 일으킬 수 있다고 생각하기 때문에 내가 틀렸다면 나를 바로 잡으십시오.

+0

doneButton 포인터를 nil로 설정했지만 아직 키보드보기에 추가되어 있기 때문에 이는 작동하지 않습니다. – James

0

addHideKeyboard는 NSNotificationCenter에 추가되는 선택기로, 키보드가 나타날 때마다 완료 버튼을 다시 추가한다는 의미입니다. 이의 ViewController 밖으로 이동 때 NotificationCenter 통해 일을했기 때문에

0

, 당신은 (현재의 컨트롤러를 새로운 ViewController을로드하거나 기각하기 전에 수 있습니다.)를 observer를 제거해야

을 그럼 당신이 추가 한 가정 해 봅시다 NotificationCenterViewDidLoadxxxViewController의 "완료 버튼"이 필요합니다. 당신이 어딘가에 이런 yyyViewController로 이동하면

[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(addHideKeyboardButtonToKeyboard:) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

이제 다음 observer 첫째을 제거합니다.

-(void)showNewViewController { 

// remove the notification observer 
[[NSNotificationCenter defaultCenter] removeObserver:self 
               name:UIKeyboardWillShowNotification 
               object:nil]; 

yyyViewController *vctrllr = [[yyyViewController alloc] init]; 
... 
... 
} 

내가 알고 있듯이, 그것의 일종의 역사 질문이지만, 이것에 대해서는 지금 당장주의를 기울였다.

관련 문제