2014-05-22 220 views
1

请耐心等待,因为我仍在学习目标c的绳索。显示隐藏按钮

我目前有一个按钮,当按下隐藏iOS键盘。我想知道如何才能做到与此相反。当选择文本字段时,键盘自动出现 - 同时,我希望此按钮也出现在屏幕上。

谢谢!

-(IBAction)done:(id)sender{ 
//... 
//... 
[Screen resignFirstResponder]; 
done.hidden = YES; 
}; 

回答

0

您将不得不使用NSNotification。实施addObserverviewWillAppear中removeObserverviewWillDisappear

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
} 

-(void)viewWillDisappear:(BOOL)animated{ 
    [super viewWillDisappear:animated]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
} 

-(void)keyboardWillShow:(NSNotification*)notification{ 
    done.hidden = NO; 
} 
0

寄存器为UIKeyboardWillShowNotification这样的:

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

,并在显示方法按钮提供:

-(void) keyboardWillShow:(NSNotification*)notification{ 
    done.hidden = NO; 
} 

不要忘了与去除观察员的dealloc

[[NSNotificationCenter defaultCenter] removeObserver:self]; 
+0

确保删除dealloc中的观察者。 – Logan

+0

当然 - 谢谢!我会更新我的答案,包括这一点,我认为这是明确的;) – robert

+0

是的,它可能是,但以防万一初学者偶然发现这个答案,我认为它可能是有用的,包括它:) – Logan

0

您可以使用UITextFieldDelegate方法来实现这一目标,例如:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    done.hidden = NO;  
    return YES; 
} 

希望这将有助于。