2012-02-10 34 views
29

Tôi có một ứng dụng iPad có biểu mẫu đăng ký bên trong nó. Biểu mẫu này rất cơ bản và chỉ chứa hai UITextFields cho Tên & Địa chỉ email.Di chuyển lên UITextField tiếp theo khi 'Tiếp theo' được khai báo

TextField đầu tiên là dành cho tên ứng cử viên, khi họ nhập tên của họ vào và nhấn 'Tiếp theo' trên bàn phím, tôi muốn điều này tự động chuyển sang TextField địa chỉ email tiếp theo để chỉnh sửa.

Bất kỳ ý tưởng nào về cách tôi có thể đặt nút tiếp theo để bàn phím chuyển sang bàn phím tiếp theo?

Cảm ơn

Trả lời

72

Bạn cần phải thực hiện điều khiển xem bạn các đại biểu UITextField, và thực hiện các UITextField phương pháp đại biểu:

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    if (textField == nameField) { 
     [textField resignFirstResponder]; 
     [emailField becomeFirstResponder]; 
    } else if (textField == emailField) { 
     // here you can define what happens 
     // when user presses return on the email field 
    } 
    return YES; 
} 

Swift phiên bản:

func textFieldShouldReturn(textField: UITextField) -> Bool { 
    if textField == nameField { 
     textField.resignFirstResponder() 
     emailField.becomeFirstResponder() 
    } else if textField == emailField { 
     // here you can define what happens 
     // when user presses return on the email field 
    } 
    return true 
} 

Bạn cũng có thể muốn để cuộn chế độ xem của bạn cho emailField để hiển thị. Nếu bộ điều khiển xem của bạn là một thể hiện của UITableViewController, điều này sẽ xảy ra tự động. Nếu không, bạn nên đọc this Apple document, đặc biệt là Di chuyển nội dung nằm dưới bàn phím một phần.

+0

Cảm ơn lawicko. Mã hoạt động hoàn hảo với trường văn bản email được đặt thành [email resignFirstResponder] ;. Cảm ơn một lần nữa. –

+3

@lawicko Câu lệnh '[textField resignFirstResponder];' trong mã của bạn ở trên không quan trọng. Trong khi vẫn còn các trường trong biểu mẫu, chỉ cần di chuyển 'trở thànhRirstResponder' sang trường tiếp theo:' [emailField trở thànhFirstResponder]; ' – Malloc

+0

@Malloc Bạn đúng, đối với các trường hợp đơn giản mà' textField' không phải là một lớp con của UITextField và không thực hiện bất kỳ điều gì cụ thể khi trạng thái trả lời đầu tiên bị từ bỏ. Tuy nhiên, mã của tôi hoạt động tốt trong mọi trường hợp, kể cả mã phức tạp hơn. Xem [tại đây] (http://developer.apple.com/library/ios/documentation/uikit/reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/resignFirstResponder) để tìm hiểu lý do tại sao resighFirstResponder' có thể bị ghi đè. – lawicko

10

Ngoài ra để @lawicko 's câu trả lời tôi thường thay đổi văn bản nút để cung cấp cho rằng kết thúc thức liên lạc (ví dụ nói next khi có nhiều lĩnh vực và sau đó done khi trên người cuối cùng):

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    BOOL isLastTextField = //.. your logic to figure out if the current text field is the last 

    if (isLastTextField) { 
     textField.returnKeyType = UIReturnKeyDone; 
    } else { 
     textField.returnKeyType = UIReturnKeyNext; 
    } 
} 
0
- (BOOL) textFieldShouldReturn:(UITextField *)textField 
{ 
    if (textField == self.textFieldName) 
    { 
     [self.textFieldName resignFirstResponder]; 
     [self.textFieldPassword becomeFirstResponder]; 
    } 
    else if (textField == self.textFieldPassword) 
    { 
     [self.textFieldPassword resignFirstResponder]; 

     [self login:self]; 
    } 

    return true; 
} 




@interface MLLoginViewController()<UITextFieldDelegate> 

@end 

@implementation MLLoginViewController 



- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.textFieldName.delegate = self; 
    self.textFieldPassword.delegate = self; 
0

Hãy một lối thoát cho các textfield, sau đó

viewController.h

(IBAction)textFieldDoneEditing:(id)sender; 

viewController.m

(IBAction)textFieldDoneEditing:(id)sender { 
    [textField resignFirstResponder]; 
    if (textField == nameField) { 
     [emailField becomeFirstResponder]; 
    } 
} 

Tận dụng mối quan hệ giữa (hiển thị các kết nối thanh tra> Sự kiện Sent) didEndOnExit và textFieldDoneEditing

5

Một cách nhất quán hơn và mạnh mẽ là sử dụng NextResponderTextField Bạn có thể cấu hình nó hoàn toàn từ trình xây dựng giao diện.

Tất cả bạn cần làm là

  1. Đặt kiểu lớp của bạn UITextFieldNextResponderTextField enter image description here
  2. Sau đó thiết lập các cửa hàng của nextResponderField để trỏ đến responder tiếp theo nó có thể là bất cứ điều gì UITextField hoặc bất kỳ lớp con nào UIResponder. Nó cũng có thể là UIButton và thư viện đủ thông minh để kích hoạt sự kiện TouchUpInside của nút chỉ khi nó được bật. enter image description here enter image description here

Dưới đây là thư viện trong hành động:

enter image description here

+0

Chính xác những gì tôi đang tìm kiếm, thx! –

8

phiên bản Swift của câu trả lời đúng.

Theo kinh nghiệm của tôi, bạn không cần phải từ chứcFirstResponder khi chuyển textFields.

Trong ví dụ này, nó chỉ là tên người dùng cơ bản và mật khẩu của bạn textFields.

Bàn phím "phím trả về" trong bảng phân cảnh cho tên người dùng được đặt thành "Tiếp theo" và mật khẩu cho mật khẩu được đặt thành "Xong".

enter image description here

Sau đó, chỉ cần kết nối các đại biểu đối với các lĩnh vực văn bản hai và thêm phần mở rộng này và bạn đang thực hiện khá nhiều.

extension LoginViewController: UITextFieldDelegate { 

    func textFieldShouldReturn(textField: UITextField) -> Bool { 
     if textField == textFieldPassword { 
      self.view.endEditing(true) 
     } else { 
      textFieldPassword.becomeFirstResponder() 
     } 
     return true 
    } 

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