2015-08-19 19 views
5

Tôi đang thử nghiệm một vấn đề với các phím số khi popping UIViewController hiện tại từ UINavigationController.Bàn phím xuất hiện ngắn gọn khi popping viewcontroller iOS

Trong UIViewController hiện tại của tôi. Tôi có một vài UITextField và một nút "Save" trong UINavigationBar. Hành vi dự kiến ​​như sau:

Khi người dùng nhấn "Lưu", bàn phím phải ẩn và thao tác mạng được thực hiện. Trong gọi lại của nó, một UIAlertView được hiển thị. Khi người sử dụng gạt bỏ UIAlertView này, một thông báo tăng và UIViewController hiện thực

[self.navigationController popViewControllerAnimated:YES]; 

Vấn đề là, nếu "Lưu" được nhấn với bàn phím vẫn hiển thị, sau khi thực hiện popViewControllerAnimated, bàn phím xuất hiện một thời gian ngắn và từ trái sang bên phải (như thể nó đã được nhìn thấy trong UIViewController trước đó).

Tôi đã thử

[myTextField resignFirstResponder] 

khi người dùng chạm vào "Lưu", khi người dùng gạt bỏ UIAlertView, và ngay cả trong những phương pháp

viewWillDisappear 

. Một số câu trả lời khác đề xuất sử dụng

[self.view endEditing:YES]; 

nhưng không hoạt động.

Nếu tôi có thể sử dụng bàn phím thông thường nó sẽ là tầm thường để ghi đè

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 

    return YES; 
} 

để ẩn nó khi người dùng chạm "Return", "Xong", vv Nhưng là phím số không cho phép bạn để hiển thị nút "kết thúc".

Tôi đánh giá cao bất kỳ sự giúp đỡ, cảm ơn tất cả các bạn đã dành thời gian

+0

bạn cần thêm nút thực hiện/hủy vào bàn phím số của mình. –

+0

Sử dụng [self.view endEditing: YES]; trước khi [tự.navigationController popViewControllerAnimated: YES]; – Dev

+0

@ T_77 Tôi đã thử [myTextField setReturnKeyType: UIReturnKeyDone]; và nó không hoạt động – LuisSedano

Trả lời

1

Hãy thử điều này:

Đặt các đại biểu trường văn bản và kiểu trả về của nó là Xong và pad như phím số.

_textField.delegate = self; 
_textField.keyboardType = UIKeyboardTypeDecimalPad; 
[_textField setReturnKeyType:UIReturnKeyDone]; 

và bây giờ thêm các nút để bàn phím:

-(void)addButtonsToKeyboard 
{ 
    UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init]; 
    [keyboardDoneButtonView sizeToFit]; 


    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil) 
                    style:UIBarButtonItemStyleDone target:self 
                    action:@selector(kbDoneAction:)]; 

    UIBarButtonItem* seperatorItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 


    UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", nil) 
                    style:UIBarButtonItemStylePlain target:self 
                    action:@selector(kbCancelAction:)]; 


    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:cancelButton,seperatorItem, doneButton, nil]]; 
    _textField.inputAccessoryView = keyboardDoneButtonView; 
} 

và để ẩn bàn phím:

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    return YES; 
} 

và phương pháp hành động Done của bạn là:

-(void)kbDoneAction:(id)sender 
{ 
    [_textField resignFirstResponder]; 
} 

và Phương thức hành động hủy là:

-(void)kbCancelAction:(id)sender 
{ 
[_textField resignFirstResponder]; 
    } 
+0

Đừng quên gọi phương thức addButtons trên viewDidload –

+0

Vẫn không hoạt động. Để giải quyết vấn đề này, tôi đã vô hiệu hóa nút lưu cho đến khi kDoneAction được gọi, nhưng nó bỏ lỡ điểm ẩn nó khi nhấn "Lưu" – LuisSedano

0

Tôi đã có một tình huống tương tự. Tôi đã kết thúc việc trì hoãn popViewControllerAnimated lâu hơn một chút so với thời lượng hoạt ảnh của bàn phím (0.333 giây).

0
Try using the below code. It works fine for iOS 8 and below version 

    if (IS_OS_8_OR_LATER) { 
      UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert]; 

      UIAlertAction *cancelAction = [UIAlertAction 
             actionWithTitle:B_title 
             style:UIAlertActionStyleCancel 
             handler:^(UIAlertAction *action) 
             { 
              [self.navigationController popViewControllerAnimated:YES]; 

             }]; 
     [alertVC addAction:cancelAction]; 
     [self presentViewController:alertVC animated:YES completion:nil]; 
     } 
     else{ 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
      [alert show]; 
     } 
} 
Các vấn đề liên quan