2012-05-22 161 views
1

是我的代碼完成按鈕不工作

- (void)keyboardWillShow:(NSNotification *)note { 
     // create custom button 
     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 found, add the button 
     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
      if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
       [keyboard addSubview:doneButton]; 
     } else { 
      if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
       [keyboard addSubview:doneButton]; 
     } 
    } 
} 

,但我無法添加完成按鈕,經過與斷點交叉檢查後,我觀察到控制不進入if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)條件。 我正在使用IOS5。

+0

爲什麼objectAtIndex:1? – stark

+0

@stark其實我正在學習本教程,http://www.neoos.ch/blog/37-uikeyboardtypenumberpad-and-the-missing-return-key – Valli

+0

你確定這個方法在鍵盤出現時被調用嗎? – Novarg

回答

2

我也在我的一個項目中添加了自定義完成按鈕。我使用的教程中提到這段代碼:

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) 
{ 
    if([[tmpKeyboard description] hasPrefix:@"<UIPeripheralHost"] == TRUE) 
     [tmpKeyboard addSubview:doneButton]; 
} 
else 
{ 
    if([[tmpKeyboard description] hasPrefix:@"<UIKeyboard"] == TRUE) 
     [tmpKeyboard addSubview:doneButton]; 
} 

此前的iOS版本3.2 UIKeyboard你的做法是好的,但後來你必須將其更改爲UIPeripheralHost。

+0

是的..我也試過這個...但是控制沒有進入if條件。那就是我無法運行[tmpKeyboard addSubview:doneButton];聲明天氣在if或else條件下。 – Valli

+0

可能是我誤解了,但是你是否改變了你的條件if([[tmpKeyboard description] hasPrefix:@「 TRD

+0

是的..但沒有運氣..我試過這個.. – Valli