views:

588

answers:

2

Hey guys,

I've set up a Button and add it to a view. I want to add a "done" button to the UIKeyboardTypeNumberPad. Here's my code.

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 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 view found; add the custom button to it
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            [keyboard addSubview:doneButton];
    }

Everything works great until I want to remove the button if I've got a Kayboard of type NumbersAndPunctuation for example.

If I click the button I use [(UIButton)*sender removeFromSuperview]; to prevent memory leaks.

But how do I remove the button from within an other function?

Thanks a lot!

Some other guys did ask that question somewhere else but didn't get a answer. I'am sure you can help :)

+1  A: 

You should store a reference to the button, instead of using a local variable. For example:

Header file:

@interface myObject : NSObject {
    UIButton   *doneButton;
    ...

Implementation file:

doneButton = [UIButton buttonWithType: UIButtonTypeCustom
...

To remove it (assuming you're in the same object:

[doneButton removeFromSuperview];

However, Apple may not take kindly to you adding buttons to their keyboard.

Ben Gottlieb
Thank you,I already tried that without success. My whole keyboard gets removed...Some other devs successfuly submitted apps with extra buttons :)Hope mine is accepted, too.
rdesign
A: 

you can declare your button in your .h file, so you will be able to get access from all class methods

Morion
thanks to you as well, but I've tried that already. It confuses me but, it removed the whole keyboard...
rdesign
you both were right. I've set up a new app. I coded it from ground up and it worked. ...confusing. thanks a lot!
rdesign