2012-01-03 3 views
2

나는 두 개의 편집 텍스트를 가지고 있습니다. 편집 텍스트 입력은 숫자 만 허용하도록 숫자를 입력해야합니다. 숫자 패드에는 완료 버튼이 없습니다. 그래서 지금은 버튼을 추가 완료 using this link 지금 완료 버튼을 작동 fine.my 문제는 내 하나 더 편집 텍스트는 일반 키보드입니다 또한 키 보드를 보여주는 것입니다. 해당 특정 편집 텍스트에서 버튼을 피하기 위해.아이폰의 일반 키패드에서 삭제 된 버튼

참고 : enter image description here 친절

당신이 코드 아래 사용할 수 있습니다

답변

2
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) 
{ 
     [[NSNotificationCenter defaultCenter] addObserver:self  
               selector:@selector(keyboardDidShow:) 
                name:UIKeyboardDidShowNotification 
               object:nil]; 
} 
else 
{ 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(keyboardWillShow:) 
                name:UIKeyboardWillShowNotification 
               object:nil]; 
} 

- (void)addButtonToKeyboard 
{ 
    // create custom button 

    doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame = CGRectMake(0, 163, 106, 53); 
    doneButton.adjustsImageWhenHighlighted = NO; 

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) 
    { 
     [doneButton setImage:[UIImage imageNamed:@"Btn-DoneDown-2.png"] forState:UIControlStateNormal]; 
     [doneButton setImage:[UIImage imageNamed:@"Btn-DoneUp-2.png"] forState:UIControlStateHighlighted]; 
    } 
    else 
    {   
     [doneButton setImage:[UIImage imageNamed:@"Btn-DoneDown-1.png"] forState:UIControlStateNormal]; 
     [doneButton setImage:[UIImage imageNamed:@"Btn-DoneUp-1.png"] forState:UIControlStateHighlighted]; 
    } 

    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside]; 

    // locate keyboard view 

    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    UIView* keyboard; 

    for(int i = 0 ; i < [tempWindow.subviews count] ; i++) 
    { 
     keyboard = [tempWindow.subviews objectAtIndex:i]; 
     // keyboard found, add the button 
     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) 
     { 
      if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
       [keyboard addSubview:doneButton]; 
     } 
     else 
     { 
      if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
       [keyboard addSubview:doneButton]; 
     } 
    } 
} 

- (void)keyboardWillShow:(NSNotification *)note 
{ 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) 
    { 
     [self addButtonToKeyboard]; 
    } 
} 

- (void)keyboardDidShow:(NSNotification *)note 
{ 
    if(doneButton) 
    { 
     [doneButton removeFromSuperview]; 
     doneButton = nil; 
    } 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) 
    { 
     [self addButtonToKeyboard]; 
    } 
} 

- (void)doneButton:(id)sender 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    [doneButton removeFromSuperview]; 
    doneButton = nil; 
} 
+0

마침내 그 작업이 라인은 내가 [removeObserver [NSNotificationCenter defaultCenter] : 자기] 찾고 있어요; 너무 많이 예고 해줘서 고마워. –

1

그것을 제거 ... 난 일반 키보드에서 해당 버튼을 제거하는 방법 나를 인도 당신이 300

//for remove done button from keyboard 
- (void)removeButtonFromKeyboard 
{ 
     // locate keyboard view 
     UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
     UIView* keyboard; 
     for(int i=0; i<[tempWindow.subviews count]; i++) 
     { 
      keyboard = [tempWindow.subviews objectAtIndex:i]; 
      // keyboard found, remove the button 
      if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
      { 
       [[keyboard viewWithTag:300] removeFromSuperview]; 
      } 
      else 
      { 
       if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
        [[keyboard viewWithTag:300] removeFromSuperview]; 
      }  
     } 
     isButtonAdded = NO ; 
    } 
을 button.Here하는 tag을 할당해야 당신이 완료 버튼을 원하는 여부 텍스트 제기의 태그를 통해 것을

편집 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 확인할 수 있습니다에서

. 좋아요 :

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    if (textField.tag == 1001) 
    { 
     // add button 
     // take a Boolean and make it true in your create button function 
     if(!doneButtonAdded)// if button is not added 
     { 
     // then add button 
     } 
    } 
    else if (textFiled.tag == 1002) // your normal key board 
    { 
     if(doneButtonAdded)// if button is added 
     { 
      // remove button 
      // in remove button function make doneButtonAdded to false 
     } 
    } 
} 

희망이 있습니다.

+0

체크 업데이트 대답 – Maulik