cannot remove UIButton from keyboard
I am using a numberkeyboard, and I am adding a done key to the bottem left corner. I want to remove this button from the keyboard because later I want to use a regular keyboard, and the done button is still showing up. I am globally declaring the UIButton and calling removeFromSuperview on it. Nothing happens. Also, if I call removeFromSuperview in the same method as it is added, it successfully removes the button. Here is some code.
-(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])开发者_如何学JAVA {
// 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];
}
}
}
and to remove the doneButton
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[doneButton removeFromSuperView];
}
A viable option would be to set doneButton to nil, then realloc and initialize it whenever it's needed...not quite as easy as the faulty removeFromSuperview:, but an option nonetheless. Correct me if I'm wrong because I believe the method I suggested may cause a memory leak.
addHideKeyboard was a selector being added to the NSNotificationCenter, which means every time the keyboard showed up, it would re-add the done button.
Since you have done it via NotificationCenter
, you need to remove the observer
when moving out of this viewController (may be before loading a new ViewController
or dismissing current controller.)
So let's say that you have added this NotificationCenter
in ViewDidLoad
of your xxxViewController
that needs the "Done button".
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(addHideKeyboardButtonToKeyboard:)
name:UIKeyboardWillShowNotification
object:nil];
Now, once you move to yyyViewController
somewhere like this, then remove the observer
first.
-(void)showNewViewController {
// remove the notification observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
yyyViewController *vctrllr = [[yyyViewController alloc] init];
...
...
}
I know, its a kind of history question, but it got my attention on this, only now.
精彩评论