Adding Next/Done buttons to NumberPad on IOS?

3.6k views Asked by At

I need to implement next/done buttons to number pad.

I am using the code below.

But i am taking an exception while selecting keyboard view. index 1 beyond bounds [0...0]

Here is my code;

//doneButton initialisation
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];


//the exception i am taking
else if(textField == cardNumberField)
{

    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];
        }
    }

    [mainScrollView setContentSize:CGSizeMake(0, mainScrollView.contentSize.height+185)];

    [mainScrollView setContentOffset:CGPointMake(0, mainScrollView.frame.origin.y+185)];
}
1

There are 1 answers

0
Mani On BEST ANSWER

This line lead to error: UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];.

Because you application always have one window.

Update: see this post.