2011-01-28 40 views
9

Làm thế nào chúng ta có thể thực hiện UIKeyboardTypeNumberPad để nó sẽ có một nút 'thực hiện'? Theo mặc định, nó không có.UIKeyboardTypeNumberPad mà không cần thực hiện nút

+0

Ý anh là gì bằng cách thực hiện bàn phím, Bạn muốn ẩn bàn phím hoặc bất cứ điều elss. – Ishu

+0

Không ... Chỉ cần sẽ không có nút được thực hiện trong bàn phím – Abhinav

+1

Bàn phím số thân yêu không có nút Xong. Vì vậy, câu hỏi của bạn không hợp lệ. – Ishu

Trả lời

13

Nếu tôi không sai thì bạn muốn hỏi cách thêm nút "Xong" tùy chỉnh vào bàn phím cho UIKeyboardTypeNumberPad. Trong trường hợp đó, điều này có thể hữu ích. Khai báo một in.h UIButton * doneButton và thêm mã sau vào tập tin .m

- (void)addButtonToKeyboard { 
    // create custom button 
    if (doneButton == nil) { 
     doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)]; 
    } 
    else { 
     [doneButton setHidden:NO]; 
    } 

    [doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    // locate keyboard view 
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    UIView* keyboard = nil; 
    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]; 
     } 
    } 
} 

- (void)doneButtonClicked:(id)Sender { 
//Write your code whatever you want to do on done button tap 
//Removing keyboard or something else 
} 

Tôi đang sử dụng như nhau trong ứng dụng của tôi và khung của nút được như vậy, điều chỉnh, Bạn do đó có thể gọi [tự addButtonToKeyboard] bất cứ khi nào bạn cần hiển thị nút xong trên bàn phím. UIKeyboardTypeNumberPad không có nút Xong nếu không. enter image description here

+0

Tôi gặp sự cố với dòng UIWindow * tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex: 1]; tôi nhận được mảng emty không có subviews – user784625

+0

Trường hợp chính xác bạn nhận được một mảng trống. Trong trường hợp bàn phím có mặt trên màn hình, bạn sẽ nhận được ít nhất một mục trong mảng để chắc chắn là bàn phím. –

+0

Tôi đã thêm cuộc gọi vào chức năng này một lần trong keyboardwilllshow và keyboarddidshow nhưng tôi vẫn nhận được mảng trống – user784625

1

Vấn đề iOS 5 được giải quyết. Cảm ơn rất nhiều.

Tôi gặp sự cố khi hiển thị nút Xong.

thay thế:

if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
    [keyboard addSubview:doneButton]; 

mà không hiển thị nút thực hiện trong iOS 5 ...

Với:

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

Làm việc một điều trị. Bạn là một ngôi sao. Cảm ơn, Nicola ;-)

1

Nếu ai đó gặp sự cố khi nút DONE tiếp tục xuất hiện khi bạn cố gắng tải các loại bàn phím khác trong cùng một ứng dụng, tôi biết rất nhiều ứng dụng có vấn đề này. Dù sao, đây là cách tôi giải quyết điều này: Trong ViewController của bạn (cùng một bộ điều khiển xem mà bạn đã thêm nút DONE), hãy thêm mã này (như là) và nó sẽ giải quyết vấn đề của bạn với nút DONE tiếp tục xuất hiện lại. Hy vọng nó sẽ giúp ích cho một số người.

- (void)viewWillDisappear:(BOOL)animated { 
[super viewWillDisappear:animated]; 
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil]; 

} else { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
} 

}

2
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil]; 


//Call this method 

- (void)keyboardWillShow:(NSNotification *)note { 


    UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)]; 
    doneButton.adjustsImageWhenHighlighted = NO; 
    [doneButton setImage:[UIImage imageNamed:@"Done.png"] forState:UIControlStateNormal]; 
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside]; 
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    UIView* keyboard; 
    for(int i=0; i<[tempWindow.subviews count]; i++) { 
     keyboard = [tempWindow.subviews objectAtIndex:i]; 

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

} 
Các vấn đề liên quan