2013-12-20 21 views
0

我需要執行下一個/完成按鈕到數字鍵盤。在IOS上向NumberPad添加Next/Done按鈕?

我使用的是下面的代碼。

但我在選擇鍵盤視圖時發生異常。 index 1 beyond bounds [0 ... 0]

這是我的代碼;

//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

檢查此: - http://stackoverflow.com/questions/584538/how-to-show-button-done-on-number-pad-on-iphone –

+0

if([[keyboard description] hasPrefix:@「

回答

1

此行導致錯誤:UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

因爲你的應用程序總是有one窗口。

更新:看到這個post