{"id":7507,"date":"2025-06-01T13:55:34","date_gmt":"2025-06-05T06:46:34","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7507"},"modified":"2025-06-05T13:55:34","modified_gmt":"2025-06-05T06:46:34","slug":"https-badgameshow-com-steven-327","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-327\/","title":{"rendered":"\u6700\u65b0 Swift \u6587\u5b57\u8f38\u5165\u6aa2\u67e5\uff1a\u4f7f\u7528 UITextField Delegate \u5b8c\u6574\u6559\u5b78\uff082025\uff09"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITextField Delegate, iOS \u958b\u767c, \u6587\u5b57\u8f38\u5165\u6aa2\u67e5, Swift \u6559\u5b78\"><\/p>\n<h1>\u6700\u65b0 Swift \u6587\u5b57\u8f38\u5165\u6aa2\u67e5\uff1a\u4f7f\u7528 UITextField Delegate \u5b8c\u6574\u6559\u5b78\uff082025\uff09<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u73fe\u4ee3\u5316\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u7528\u65bc iOS \u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u3002\u7576\u958b\u767c\u8005\u5e0c\u671b\u5c0d\u4f7f\u7528\u8005\u8f38\u5165\u7684\u6587\u5b57\u9032\u884c\u6aa2\u67e5\u6642\uff0c<strong>UITextField Delegate<\/strong> \u63d0\u4f9b\u4e86\u4e00\u500b\u6709\u6548\u7684\u65b9\u6cd5\u4f86\u5be6\u73fe\u9019\u4e00\u9700\u6c42\uff0c\u78ba\u4fdd\u8f38\u5165\u7684\u5167\u5bb9\u7b26\u5408\u61c9\u7528\u7a0b\u5f0f\u7684\u8981\u6c42\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f UITextField Delegate\uff1f<\/h2>\n<p>UITextField Delegate \u662f iOS \u4e2d\u4e00\u500b\u5c08\u9580\u7528\u4f86\u8655\u7406\u6587\u5b57\u8f38\u5165\u7684\u5354\u8b70\u3002\u900f\u904e\u9019\u500b\u5354\u8b70\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u7ba1\u7406\u6587\u5b57\u6846\u7684\u884c\u70ba\uff0c\u4e26\u5728\u7528\u6236\u8f38\u5165\u6642\u9032\u884c\u6aa2\u67e5\u3002<\/p>\n<h2>\u5982\u4f55\u5be6\u4f5c UITextField Delegate<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 UITextField Delegate \u9032\u884c\u6587\u5b57\u8f38\u5165\u6aa2\u67e5\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u7684 UIViewController \u9075\u5faa <strong>UITextFieldDelegate<\/strong> \u5354\u8b70\u3002<\/li>\n<li>\u5728\u4f60\u7684 ViewController \u4e2d\uff0c\u5c07 UITextField \u7684 delegate \u8a2d\u5b9a\u70ba self\u3002<\/li>\n<li>\u5be6\u4f5c <strong>textField(_:shouldChangeCharactersIn:replacementString:)<\/strong> \u65b9\u6cd5\uff0c\u9032\u884c\u6587\u5b57\u6aa2\u67e5\u3002<\/li>\n<\/ol>\n<h3>\u7bc4\u4f8b\u7a0b\u5f0f\u78bc<\/h3>\n<pre><code class=\"language-swift line-numbers\">import UIKit\n\nclass ViewController: UIViewController, UITextFieldDelegate {\n\n    @IBOutlet weak var textField: UITextField!\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        textField.delegate = self\n    }\n\n    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {\n        \/\/ \u6aa2\u67e5\u8f38\u5165\u662f\u5426\u70ba\u6578\u5b57\n        let allowedCharacters = CharacterSet.decimalDigits\n        let characterSet = CharacterSet(charactersIn: string)\n        return allowedCharacters.isSuperset(of: characterSet)\n    }\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u4f5c UITextField Delegate \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u672a\u5c07 delegate \u8a2d\u5b9a\u70ba\u81ea\u8eab\uff0c\u5c0e\u81f4\u65b9\u6cd5\u7121\u6cd5\u8abf\u7528\u3002<\/li>\n<li>\u672a\u6b63\u78ba\u6aa2\u67e5\u8f38\u5165\u5b57\u7b26\u7684\u96c6\u5408\uff0c\u53ef\u80fd\u5c0e\u81f4\u4e0d\u5fc5\u8981\u7684\u8f38\u5165\u88ab\u63a5\u53d7\u3002<\/li>\n<li>\u5fd8\u8a18\u5be6\u4f5c <strong>textFieldDidBeginEditing<\/strong> \u548c <strong>textFieldDidEndEditing<\/strong> \u65b9\u6cd5\uff0c\u53ef\u80fd\u6703\u5f71\u97ff\u7528\u6236\u9ad4\u9a57\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u6587\u5b57\u6aa2\u67e5\uff0cUITextField Delegate \u9084\u53ef\u4ee5\u64f4\u5c55\u61c9\u7528\u65bc\uff1a<\/p>\n<ul>\n<li>\u9650\u5236\u8f38\u5165\u5b57\u6578<\/li>\n<li>\u5373\u6642\u986f\u793a\u932f\u8aa4\u63d0\u793a<\/li>\n<li>\u6839\u64da\u4e0d\u540c\u8f38\u5165\u72c0\u6cc1\u8abf\u6574 UI \u5143\u7d20\uff08\u5982\u5b57\u9ad4\u984f\u8272\uff09<\/li>\n<\/ul>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u900f\u904e UITextField Delegate\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\u6587\u5b57\u8f38\u5165\u6aa2\u67e5\uff0c\u5f9e\u800c\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u7528\u6236\u9ad4\u9a57\u3002\u5efa\u8b70\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u7d50\u5408\u6700\u4f73\u5be6\u8e10\u4f86\u64f4\u5c55\u529f\u80fd\uff0c\u78ba\u4fdd\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u8207\u6613\u7528\u6027\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u6587\u5b57\u8f38\u5165\u6aa2\u67e5-UITextField-Delegate_1675476841.080777.webp\" alt=\"Swift \u6587\u5b57\u8f38\u5165\u6aa2\u67e5 UITextField Delegate\" title=\"Swift\u6587\u5b57\u8f38\u5165\u6aa2\u67e5\u3001UITextField Delegate\u3001\u6587\u5b57\u8f38\u5165\u6aa2\u67e5\u65b9\u6cd5\u3001Swift\u6587\u5b57\u8f38\u5165\u6aa2\u67e5\u529f\u80fd\u3001UITextField Delegate\u6587\u5b57\u8f38\u5165\u6aa2\u67e5\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UITextField Delegate \u5982\u4f55\u5f71\u97ff\u7528\u6236\u9ad4\u9a57\uff1f<\/h3>\n<p>UITextField Delegate \u80fd\u5920\u5373\u6642\u6aa2\u67e5\u7528\u6236\u8f38\u5165\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u932f\u8aa4\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h3>2. \u5982\u4f55\u9650\u5236 UITextField \u7684\u6700\u5927\u8f38\u5165\u5b57\u6578\uff1f<\/h3>\n<p>\u53ef\u4ee5\u5728 <strong>textField(_:shouldChangeCharactersIn:replacementString:)<\/strong> \u65b9\u6cd5\u4e2d\u6aa2\u67e5\u7576\u524d\u5b57\u6578\uff0c\u4e26\u6839\u64da\u9700\u8981\u8fd4\u56de true \u6216 false\u3002<\/p>\n<h3>3. UITextField Delegate \u662f\u5426\u9069\u7528\u65bc\u6240\u6709\u7248\u672c\u7684 Swift\uff1f<\/h3>\n<p>\u662f\u7684\uff0cUITextField Delegate \u5728\u6240\u6709\u652f\u63f4 Swift \u7684 iOS \u7248\u672c\u4e2d\u90fd\u6709\u6548\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684UITextFieldDelegate\u6587\u5b57\u8f38\u5165\u6aa2\u67e5\uff0c\u4e86\u89e3\u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528\u6587\u5b57\u8f38\u5165\u6aa2\u67e5\u7684\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528UITextFieldDelegate\u4f86\u5be6\u73fe\u6587\u5b57\u8f38\u5165\u6aa2\u67e5\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":7506,"comment_status":"open","ping_status":"closed","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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-7507","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u6587\u5b57\u8f38\u5165\u6aa2\u67e5-UITextField-Delegate_1675476841.080777.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1X5","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7507","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=7507"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7507\/revisions"}],"predecessor-version":[{"id":13621,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7507\/revisions\/13621"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7506"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}