{"id":7593,"date":"2025-06-03T15:20:44","date_gmt":"2025-06-05T07:36:44","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7593"},"modified":"2025-06-05T15:20:44","modified_gmt":"2025-06-05T07:36:44","slug":"https-badgameshow-com-steven-448","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-448\/","title":{"rendered":"\u30102025 \u6700\u65b0 Swift \u641c\u7d22\u6b04\u5be6\u4f5c\u6307\u5357 | \u5b8c\u6574\u7684\u641c\u7d22\u529f\u80fd\u6559\u5b78\u3011"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u641c\u7d22\u6b04, \u641c\u7d22\u529f\u80fd\u5be6\u4f5c\"><\/p>\n<h2>2025 \u6700\u65b0 Swift \u641c\u7d22\u6b04\u5be6\u4f5c\u6307\u5357<\/h2>\n<p>\u5728\u958b\u767c iOS App \u6642\uff0c\u641c\u7d22\u529f\u80fd\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u5143\u4ef6\uff0c\u80fd\u5920\u5e6b\u52a9\u4f7f\u7528\u8005\u5feb\u901f\u627e\u5230\u60f3\u8981\u7684\u8cc7\u6599\u3002\u5728 Swift \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>UISearchBar<\/strong> \u4f86\u5be6\u4f5c\u9019\u500b\u529f\u80fd\u3002\u672c\u6587\u5c07\u63d0\u4f9b\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u4f60\u6709\u6548\u5730\u5be6\u4f5c\u641c\u7d22\u529f\u80fd\u3002<\/p>\n<h2>\u4f7f\u7528 UISearchBar \u5be6\u4f5c\u641c\u7d22\u529f\u80fd<\/h2>\n<p>UISearchBar \u662f\u4e00\u500b\u53ef\u4ee5\u8b93\u4f7f\u7528\u8005\u8f38\u5165\u6587\u5b57\u7684\u641c\u7d22\u6b04\u3002\u4e0b\u9762\u662f\u5be6\u4f5c\u6b65\u9a5f\uff1a<\/p>\n<p>1. **\u5728 Storyboard \u4e2d\u6dfb\u52a0 UISearchBar**<br \/>\n   \u5c07 UISearchBar \u62d6\u66f3\u5230\u4f60\u7684 ViewController \u4e2d\u3002<\/p>\n<p>2. **\u8a2d\u7f6e\u4ee3\u7406**<br \/>\n   \u5728 ViewController \u4e2d\u52a0\u5165 UISearchBarDelegate\uff0c\u4e26\u628a UISearchBar \u7684 delegate \u6307\u5b9a\u70ba ViewController\uff0c\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a<\/p>\n<p>   &#8220;`swift<br \/>\n   class ViewController: UIViewController, UISearchBarDelegate {<br \/>\n       @IBOutlet weak var searchBar: UISearchBar!<\/p>\n<p>       override func viewDidLoad() {<br \/>\n           super.viewDidLoad()<br \/>\n           searchBar.delegate = self<br \/>\n       }<br \/>\n   }<br \/>\n   &#8220;`<\/p>\n<p>3. **\u5be6\u4f5c\u641c\u5c0b\u908f\u8f2f**<br \/>\n   \u4f7f\u7528 `textDidChange` \u65b9\u6cd5\u4f86\u76e3\u63a7\u4f7f\u7528\u8005\u7684\u8f38\u5165\uff0c\u4e26\u9032\u884c\u76f8\u61c9\u7684\u641c\u7d22\u64cd\u4f5c\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>   &#8220;`swift<br \/>\n   func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {<br \/>\n       \/\/ \u5728\u9019\u88e1\u5be6\u4f5c\u641c\u5c0b\u7684\u52d5\u4f5c\uff0c\u6bd4\u5982\u904e\u6ffe\u8cc7\u6599<br \/>\n       if searchText.isEmpty {<br \/>\n           \/\/ \u6e05\u9664\u641c\u7d22\u7d50\u679c<br \/>\n       } else {<br \/>\n           \/\/ \u57f7\u884c\u641c\u7d22\u908f\u8f2f\uff0c\u904e\u6ffe\u8cc7\u6599<br \/>\n       }<br \/>\n   }<br \/>\n   &#8220;`<\/p>\n<p>4. **\u8655\u7406\u641c\u5c0b\u6309\u9215\u9ede\u64ca**<br \/>\n   \u7576\u4f7f\u7528\u8005\u6309\u4e0b\u641c\u5c0b\u6309\u9215\u6642\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u65b9\u6cd5\u8655\u7406\uff1a<\/p>\n<p>   &#8220;`swift<br \/>\n   func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {<br \/>\n       \/\/ \u5728\u9019\u88e1\u57f7\u884c\u641c\u5c0b\uff0c\u4f8b\u5982\u67e5\u8a62\u8cc7\u6599\u5eab<br \/>\n       searchBar.resignFirstResponder() \/\/ \u6536\u56de\u9375\u76e4<br \/>\n   }<br \/>\n   &#8220;`<\/p>\n<p>\u9019\u6a23\uff0c\u4f7f\u7528\u8005\u5c31\u80fd\u5920\u901a\u904e UISearchBar \u4f86\u9032\u884c\u641c\u7d22\uff0c\u5feb\u901f\u627e\u5230\u6240\u9700\u7684\u8cc7\u6599\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u5e38\u898b\u554f\u984c<\/h2>\n<p>&#8211; **\u641c\u5c0b\u7d50\u679c\u4e0d\u986f\u793a**\uff1a\u78ba\u8a8d UISearchBar \u7684 delegate \u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\uff0c\u4e26\u6aa2\u67e5\u641c\u5c0b\u908f\u8f2f\u662f\u5426\u6709\u8aa4\u3002<br \/>\n&#8211; **\u9375\u76e4\u672a\u80fd\u81ea\u52d5\u6536\u56de**\uff1a\u78ba\u4fdd\u5728\u641c\u5c0b\u6309\u9215\u88ab\u9ede\u64ca\u5f8c\u8abf\u7528 `resignFirstResponder()` \u65b9\u6cd5\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>&#8211; \u53ef\u4ee5\u5c07 UISearchBar \u8207 UITableView \u7d50\u5408\uff0c\u8b93\u4f7f\u7528\u8005\u53ef\u4ee5\u5728\u5217\u8868\u4e2d\u5feb\u901f\u904e\u6ffe\u9805\u76ee\u3002<br \/>\n&#8211; \u82e5\u8981\u5728 UISearchBar \u4e2d\u986f\u793a\u5efa\u8b70\uff0c\u53ef\u4ee5\u4f7f\u7528 `UISearchResultsUpdating` \u5354\u8b70\u4f86\u5be6\u4f5c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/\ud83d\udd0dSwift-\u641c\u7d22\u6b04-\u641c\u7d22\u529f\u80fd\u5be6\u4f5c-\ud83d\udd0d_1675477719.071435.webp\" alt=\"\ud83d\udd0dSwift \u641c\u7d22\u6b04 | \u641c\u7d22\u529f\u80fd\u5be6\u4f5c \ud83d\udd0d\" title=\"Swift \u641c\u7d22\u6b04\u3001\u641c\u7d22\u529f\u80fd\u5be6\u4f5c\u3001Swift \u641c\u7d22\u529f\u80fd\u3001Swift \u641c\u7d22\u65b9\u6cd5\u3001Swift \u641c\u7d22\u51fd\u6578\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UISearchBar \u53ef\u4ee5\u7528\u4f86\u904e\u6ffe UITableView \u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0cUISearchBar \u53ef\u4ee5\u8207 UITableView \u4e00\u8d77\u4f7f\u7528\uff0c\u8b93\u4f7f\u7528\u8005\u80fd\u5920\u5728\u5217\u8868\u4e2d\u9032\u884c\u5373\u6642\u904e\u6ffe\u3002<\/p>\n<h3>2. \u5982\u4f55\u81ea\u5b9a\u7fa9 UISearchBar \u7684\u5916\u89c0\uff1f<\/h3>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 UISearchBar \u7684\u5c6c\u6027\u4f86\u6539\u8b8a\u5916\u89c0\uff0c\u4f8b\u5982\u8a2d\u7f6e\u80cc\u666f\u8272\u3001\u6587\u5b57\u984f\u8272\u7b49\uff0c\u6216\u8005\u4f7f\u7528\u81ea\u5b9a\u7fa9\u7684 UIView \u4f86\u66ff\u4ee3\u9ed8\u8a8d\u7684\u6a23\u5f0f\u3002<\/p>\n<h3>3. \u641c\u7d22\u7d50\u679c\u7684\u6027\u80fd\u5982\u4f55\u512a\u5316\uff1f<\/h3>\n<p>\u5728\u5be6\u4f5c\u641c\u7d22\u6642\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u7570\u6b65\u67e5\u8a62\u6216\u8cc7\u6599\u5eab\u7d22\u5f15\uff0c\u4ee5\u63d0\u9ad8\u641c\u5c0b\u6027\u80fd\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u5be6\u73fe\u641c\u7d22\u529f\u80fd\uff0c\u4e86\u89e3Swift\u4e2d\u7684methods()\u51fd\u6578\u53ca\u5176\u4ed6\u641c\u7d22\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u63d0\u9ad8\u641c\u7d22\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":7592,"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-7593","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\/\ud83d\udd0dSwift-\u641c\u7d22\u6b04-\u641c\u7d22\u529f\u80fd\u5be6\u4f5c-\ud83d\udd0d_1675477719.071435.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Yt","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7593","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=7593"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7593\/revisions"}],"predecessor-version":[{"id":13317,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7593\/revisions\/13317"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7592"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}