2012-03-03 4 views
1

숫자 키패드가있는 몇 개의 UITextField가있는 작은 프로젝트가 있습니다. 키보드가 표시되면 사용자가 키보드를 닫을 수있는 하위보기로 단추를 추가합니다.응용 프로그램이 백그라운드에서 돌아 오는 것으로 돌아 오면 UIButton이 사라집니다.

그러나 키보드가 활성화되어 있고 응용 프로그램을 닫으면 추가 한 단추가 응용 프로그램을 다시 실행하면 사라집니다. (응용 프로그램은 멀티 태스킹을 통해 비활성 상태이므로 완전히 종료하지 않습니다.)

이 코드는 버튼을 추가하는 데 사용되는 코드입니다 (xib에 구성된 "완료"버튼). 당신이 - (void)keyboardWillShow에서 관찰자를 제거하기 때문에

- (void)viewDidLoad 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(keyboardWillShow)name:UIKeyboardWillShowNotification object:nil]; 

    [super viewDidLoad]; 
} 

- (void)keyboardWillShow{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 

    // We must perform on delay (schedules on next run loop pass) for the keyboard subviews to be present. 
    [self performSelector:@selector(addHideKeyboardButtonToKeyboard) withObject:nil afterDelay:0]; 
} 

- (void)addHideKeyboardButtonToKeyboard{ 
    // Locate non-UIWindow. 
    doneButton.hidden=NO; 

    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 __strong *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) { 
     [doneButton setImage:[UIImage imageNamed:@"doneupHeb.png"] forState:UIControlStateNormal]; 
     [doneButton setImage:[UIImage imageNamed:@"donedownHeb.png"] forState:UIControlStateHighlighted]; 
     doneButton.frame = CGRectMake(-1, 163, 106, 53); 
     [foundKeyboard addSubview:doneButton]; 
     // Add the button to foundKeyboard. 
    } 

} 

-(void)textFieldDidEndEditing:(UITextField *)textField{ 
    [loan resignFirstResponder]; 
    [YearCycle resignFirstResponder]; 
    [prime resignFirstResponder]; 
    [MothlyReturn resignFirstResponder]; 

    [doneButton removeFromSuperview]; 
    doneButton = nil; 
} 


- (void)textFieldDidBeginEditing:(UITextField *)textField{ 

    textField.delegate=self; 
    //editingField = textField; 

    if ([prime isFirstResponder]||[MothlyReturn isFirstResponder]){ 

     scroll.contentOffset = CGPointMake(0, 166); 
    } 
// if ([YearCycle isFirstResponder]){ 
     // scroll.contentOffset = CGPointMake(0, 200); 

} 

- (IBAction)closeNumpad:(id)sender{ 

    [loan resignFirstResponder]; 
    [YearCycle resignFirstResponder]; 
    [prime resignFirstResponder]; 
    [MothlyReturn resignFirstResponder]; 

    scroll.contentOffset = CGPointMake(0, 0); 
    doneButton.hidden=YES; 
} 

답변

0

는 내가 .. 내가 웹 사이트에 다른 질문의 도움으로 문제를 고정 -(void)viewDidUnload

+0

안녕, 답변을 주셔서 감사하지만 난 여전히 내 버튼을 lossing. –

1

이 라인 [[NSNotificationCenter defaultCenter] removeObserver:self];을 넣어하려고 문제를 생각한다 - 문제가 있거나있는 모든 사용자에게 - 이것은 코드입니다. 참고 : 버튼 자체는 코드가 아닌 xib 파일로 디자인되었습니다.

.H 파일 :

BOOL firstTime; 
BOOL add; 
BOOL keyboardOpened; 
IBOutlet UIButton *doneButton; 

하는 .m 파일 :

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    firstTime = TRUE; 
    add = TRUE; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 

    // [[NSNotificationCenter defaultCenter] removeObserver:self]; 

    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (void)addButtonToKeyboard { 
    // create custom button 
    /* UIButton *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.tag = 3; 
    [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 ([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES && add){ 
      doneButton.frame = CGRectMake(-1, 163, 106, 53); 
      [keyboard addSubview:doneButton]; 

     }  
    } 
} 

- (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:3] removeFromSuperview]; 
    } 
} 

- (IBAction)doneButton:(id)sender { 
    [loan resignFirstResponder]; 
    [YearCycle resignFirstResponder]; 
    [ageOfCeo resignFirstResponder]; 
    [YearofBusiness resignFirstResponder]; 
    scroll.contentOffset = CGPointMake(0, 0); 

    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) 
    { 
     [self removeButtonFromKeyboard]; 
     firstTime = TRUE; 
    } 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { 
    [theTextField resignFirstResponder]; 
    return YES; 
} 

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    if ([ageOfCeo isFirstResponder]||[YearofBusiness isFirstResponder]){ 

     scroll.contentOffset = CGPointMake(0, 166); 
    } 

    // firstResponder = textField; 
} 

- (void)keyboardDidShow:(id)sender 
{ 
    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) 
    { 
     NSLog(@"%@",[[UIDevice currentDevice] model]); 
     [self addButtonToKeyboard]; 
     keyboardOpened = TRUE; 
    } 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 

    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) 
    { 
     [self removeButtonFromKeyboard]; 
     keyboardOpened = FALSE; 
    } 
} 
+0

IBOutlet 인 경우 xib 또는 스토리 보드에 무언가가 지정 될 때 지정하는 것에 대해 많이 염려하지 않아도됩니다. –

관련 문제