{"id":57,"date":"2025-06-04T10:21:54","date_gmt":"2025-06-05T03:11:53","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=57"},"modified":"2025-06-05T10:21:54","modified_gmt":"2025-06-05T03:11:53","slug":"swift-textfield-becomefirstresponder-%e5%85%89%e6%a8%99%e8%b7%b3%e8%bd%89-%e8%bc%b8%e5%85%a5%e4%bd%8d%e7%bd%ae%e8%b7%b3%e8%bd%89-%e9%bc%a0%e6%a8%99%e8%b7%b3%e8%bd%89","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/uncategorized\/swift-textfield-becomefirstresponder-%e5%85%89%e6%a8%99%e8%b7%b3%e8%bd%89-%e8%bc%b8%e5%85%a5%e4%bd%8d%e7%bd%ae%e8%b7%b3%e8%bd%89-%e9%bc%a0%e6%a8%99%e8%b7%b3%e8%bd%89\/","title":{"rendered":"Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u5982\u4f55\u5728 UITextField \u4e4b\u9593\u8df3\u8f49\u5149\u6a19\u4f4d\u7f6e"},"content":{"rendered":"<h3>\u4f7f\u7528 UITextField \u5728\u8f38\u5165\u6846\u4e4b\u9593\u8df3\u8f49\u5149\u6a19<\/h3>\n<p>\u5728 Swift \u958b\u767c\u4e2d\uff0c\u7d93\u5e38\u9700\u8981\u5728\u591a\u500b `UITextField` \u4e4b\u9593\u9032\u884c\u5149\u6a19\u8df3\u8f49\uff0c\u7279\u5225\u662f\u5728\u8868\u55ae\u586b\u5beb\u6642\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 2025 \u5e74\u6700\u65b0\u8a9e\u6cd5\u4e2d\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u7684\u8a73\u7d30\u6b65\u9a5f\u3002<\/p>\n<p>### 1. \u8a2d\u5b9a UITextField<\/p>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u7684 `UITextField` \u5df2\u6b63\u78ba\u8a2d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u7bc4\uff0c\u5c55\u793a\u5982\u4f55\u5728 storyboard \u6216\u7a0b\u5f0f\u78bc\u4e2d\u521d\u59cb\u5316 `UITextField`\u3002<\/p>\n<p>&#8220;`swift<br \/>\nlet textField1 = UITextField()<br \/>\nlet textField2 = UITextField()<\/p>\n<p>textField1.placeholder = &#8220;\u8f38\u5165\u7b2c\u4e00\u500b\u6578\u64da&#8221;<br \/>\ntextField2.placeholder = &#8220;\u8f38\u5165\u7b2c\u4e8c\u500b\u6578\u64da&#8221;<\/p>\n<p>\/\/ \u8a2d\u5b9a delegate<br \/>\ntextField1.delegate = self<br \/>\ntextField2.delegate = self<br \/>\n&#8220;`<\/p>\n<p>### 2. \u5be6\u4f5c UITextFieldDelegate \u5354\u8b70<\/p>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5be6\u4f5c `UITextFieldDelegate` \u5354\u8b70\uff0c\u4e26\u91cd\u5beb `textFieldShouldReturn` \u65b9\u6cd5\u3002\u7576\u7528\u6236\u6309\u4e0b\u9375\u76e4\u4e0a\u7684 Return \u9375\u6642\uff0c\u4f60\u53ef\u4ee5\u63a7\u5236\u5149\u6a19\u7684\u8df3\u8f49\u3002<\/p>\n<p>&#8220;`swift<br \/>\nextension YourViewController: UITextFieldDelegate {<br \/>\n    func textFieldShouldReturn(_ textField: UITextField) -> Bool {<br \/>\n        if textField == textField1 {<br \/>\n            textField2.becomeFirstResponder() \/\/ \u8df3\u8f49\u5230\u7b2c\u4e8c\u500b textField<br \/>\n        } else if textField == textField2 {<br \/>\n            textField1.becomeFirstResponder() \/\/ \u8df3\u8f49\u56de\u7b2c\u4e00\u500b textField<br \/>\n        }<br \/>\n        return true<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>### 3. \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5982\u679c\u5149\u6a19\u8df3\u8f49\u672a\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<p>&#8211; \u78ba\u8a8d `UITextField` \u7684 delegate \u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\u3002<br \/>\n&#8211; \u78ba\u8a8d `textFieldShouldReturn` \u65b9\u6cd5\u662f\u5426\u88ab\u8abf\u7528\uff08\u53ef\u4ee5\u5728\u65b9\u6cd5\u5167\u52a0\u4e0a print \u8a9e\u53e5\u9032\u884c\u8abf\u8a66\uff09\u3002<br \/>\n&#8211; \u78ba\u4fdd `returnKeyType` \u8a2d\u7f6e\u70ba `.default` \u6216\u5176\u4ed6\u985e\u578b\uff0c\u4ee5\u4fbf\u7528\u6236\u80fd\u5920\u8f15\u9b06\u5730\u627e\u5230 Return \u9375\u3002<\/p>\n<p>### 4. \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u9019\u500b\u529f\u80fd\u53ef\u4ee5\u64f4\u5c55\u5230\u66f4\u591a\u7684 `UITextField`\uff0c\u53ea\u9700\u5728 `textFieldShouldReturn` \u65b9\u6cd5\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u689d\u4ef6\u5224\u65b7\u5373\u53ef\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e09\u500b `UITextField`\uff0c\u4f60\u53ef\u4ee5\u9019\u6a23\u5be6\u73fe\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nif textField == textField1 {<br \/>\n    textField2.becomeFirstResponder()<br \/>\n} else if textField == textField2 {<br \/>\n    textField3.becomeFirstResponder()<br \/>\n} else if textField == textField3 {<br \/>\n    textField1.becomeFirstResponder()<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u7528\u6236\u5c31\u53ef\u4ee5\u5728\u4e09\u500b\u8f38\u5165\u6846\u4e4b\u9593\u7121\u7e2b\u5207\u63db\u3002<\/p>\n<hr \/>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"Swift \u7a0b\u5f0f\u6559\u5b78\u793a\u7bc4\u5716\u7247\" title=\"\"><\/a><\/p>\n<p>### \u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/p>\n<p>**Q1: \u5982\u4f55\u5728 UITextField \u4e2d\u81ea\u52d5\u986f\u793a\u9375\u76e4\uff1f**<br \/>\nA1: \u4f7f\u7528 `becomeFirstResponder()` \u65b9\u6cd5\u53ef\u4ee5\u81ea\u52d5\u986f\u793a\u9375\u76e4\uff0c\u901a\u5e38\u5728\u8996\u5716\u52a0\u8f09\u6642\u8abf\u7528\u3002<\/p>\n<p>**Q2: \u5982\u679c\u6211\u6709\u591a\u500b\u8f38\u5165\u6846\uff0c\u5982\u4f55\u4f7f\u5b83\u5011\u80fd\u5920\u66f4\u65b9\u4fbf\u5730\u8df3\u8f49\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `UITextFieldDelegate` \u7684 `textFieldShouldReturn` \u65b9\u6cd5\u4f86\u63a7\u5236\u5149\u6a19\u7684\u8df3\u8f49\uff0c\u4e26\u52a0\u5165\u9069\u7576\u7684\u908f\u8f2f\u4ee5\u652f\u6301\u591a\u500b\u8f38\u5165\u6846\u4e4b\u9593\u7684\u5207\u63db\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u81ea\u5b9a\u7fa9\u9375\u76e4\u985e\u578b\uff1f**<br \/>\nA3: \u4f60\u53ef\u4ee5\u4f7f\u7528 `textField.keyboardType` \u5c6c\u6027\u4f86\u8a2d\u7f6e\u4e0d\u540c\u7684\u9375\u76e4\u985e\u578b\uff0c\u4f8b\u5982\u6578\u5b57\u9375\u76e4\u3001\u96fb\u5b50\u90f5\u4ef6\u9375\u76e4\u7b49\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 UITextField \u5728\u8f38\u5165\u6846\u4e4b\u9593\u8df3\u8f49\u5149\u6a19&#8230;<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-V","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":625,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/57\/revisions\/625"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}