{"id":6908,"date":"2025-06-02T13:40:18","date_gmt":"2025-06-05T06:10:17","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6908"},"modified":"2025-06-05T13:40:18","modified_gmt":"2025-06-05T06:10:17","slug":"https-badgameshow-com-steven-130","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-130\/","title":{"rendered":"2025 \u6700\u65b0 Swift \u6559\u5b78\uff1a\u81ea\u5b9a\u7fa9 UIViewController \u8fd4\u56de\u6309\u9215 \ud83d\udd19\ud83c\udfa8"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UIViewController, \u81ea\u5b9a\u7fa9\u8fd4\u56de\u6309\u94ae, iOS, Swift \u6559\u5b78\"><\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u5728Swift\u958b\u767c\u4e2d\uff0c\u81ea\u5b9a\u7fa9UIViewController\u7684\u8fd4\u56de\u6309\u9215\u662f\u4e00\u9805\u975e\u5e38\u5be6\u7528\u7684\u6280\u80fd\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u5347\u61c9\u7528\u7684\u7f8e\u89c0\u6027\uff0c\u9084\u80fd\u63d0\u9ad8\u4f7f\u7528\u8005\u9ad4\u9a57\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Swift\u4e2d\u81ea\u5b9a\u7fa9UIViewController\u7684\u8fd4\u56de\u6309\u9215\uff0c\u4e26\u63d0\u4f9b2025\u6700\u65b0\u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u6b65\u9a5f\u4e00\uff1a\u8a2d\u7f6e UINavigationItem<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728UIViewController\u4e2d\u5b9a\u7fa9\u4e00\u500b<strong>navigationItem<\/strong>\uff0c\u4e26\u5c07\u5176\u8a2d\u7f6e\u70ba<strong>UINavigationBar<\/strong>\u7684<strong>titleView<\/strong>\u3002\u9019\u6a23\u505a\u53ef\u4ee5\u70ba\u6211\u5011\u7684\u8fd4\u56de\u6309\u9215\u5275\u5efa\u4e00\u500b\u57fa\u672c\u7684\u7d50\u69cb\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">let navigationItem = UINavigationItem()\nnavigationItem.titleView = UIView()\nnavigationBar.setItems([navigationItem], animated: false)\n<\/code><\/pre>\n<h2>\u6b65\u9a5f\u4e8c\uff1a\u6dfb\u52a0 UIBarButtonItem<\/h2>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u53ef\u4ee5\u5728<strong>navigationItem<\/strong>\u4e2d\u6dfb\u52a0\u4e00\u500b<strong>UIBarButtonItem<\/strong>\uff0c\u4e26\u8a2d\u7f6e\u5b83\u7684<strong>title<\/strong>\u548c<strong>action<\/strong>\u3002\u9019\u662f\u81ea\u5b9a\u7fa9\u8fd4\u56de\u6309\u9215\u7684\u95dc\u9375\u6b65\u9a5f\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">let backButton = UIBarButtonItem(title: \"\u8fd4\u56de\", style: .plain, target: self, action: #selector(backButtonTapped))\nnavigationItem.leftBarButtonItem = backButton\n<\/code><\/pre>\n<h2>\u6b65\u9a5f\u4e09\uff1a\u5b9a\u7fa9\u8fd4\u56de\u52d5\u4f5c<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5728<strong>backButtonTapped<\/strong>\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u8fd4\u56de\u6309\u9215\u7684\u52d5\u4f5c\u3002\u9019\u6a23\u4f7f\u7528\u8005\u5728\u6309\u4e0b\u6309\u9215\u6642\uff0c\u61c9\u7528\u5c31\u6703\u57f7\u884c\u76f8\u61c9\u7684\u8fd4\u56de\u884c\u70ba\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">@objc func backButtonTapped() {\n    \/\/ \u8fd4\u56de\u5230\u4e0a\u4e00\u500b\u8996\u5716\u63a7\u5236\u5668\n    navigationController?.popViewController(animated: true)\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>&#8211; **\u7121\u6cd5\u8fd4\u56de**\uff1a\u5982\u679c\u8fd4\u56de\u6309\u9215\u7121\u6cd5\u6b63\u5e38\u5de5\u4f5c\uff0c\u8acb\u78ba\u4fdd\u4f60\u7684UIViewController\u6b63\u78ba\u5730\u5d4c\u5165\u5728UINavigationController\u4e2d\u3002<br \/>\n&#8211; **\u6309\u9215\u4e0d\u986f\u793a**\uff1a\u6aa2\u67e5\u4f60\u7684navigationItem\u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\uff0c\u4ee5\u53caUIBarButtonItem\u7684target\u548caction\u662f\u5426\u9023\u7d50\u6b63\u78ba\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>&#8211; \u81ea\u5b9a\u7fa9\u8fd4\u56de\u6309\u9215\u7684\u6a23\u5f0f\u8207\u5716\u6a19\uff0c\u63d0\u5347\u7528\u6236\u754c\u9762\u7684\u4e00\u81f4\u6027\u3002<br \/>\n&#8211; \u5728\u4e0d\u540c\u7684\u60c5\u5883\u4e0b\u4f7f\u7528\u4e0d\u540c\u7684\u8fd4\u56de\u6309\u9215\uff0c\u6bd4\u5982\u5728\u67d0\u4e9b\u60c5\u5883\u4e0b\u4f7f\u7528\u5716\u7247\u800c\u4e0d\u662f\u6587\u5b57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UIViewController-\u81ea\u5b9a\u7fa9\u8fd4\u56de\u6309\u94ae-\ud83d\udd19\ud83c\udfa8_1675398278.655298.webp\" alt=\"Swift UIViewController \u81ea\u5b9a\u7fa9\u8fd4\u56de\u6309\u9215 \ud83d\udd19\ud83c\udfa8\" title=\"Swift\u7a0b\u5f0f\u8a9e\u6cd5\u3001UIViewController\u3001\u81ea\u5b9a\u7fa9\u8fd4\u56de\u6309\u9215\u3001\u8a2d\u8a08\u98a8\u683c\u3001\u61c9\u7528\u7a0b\u5f0f\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u81ea\u5b9a\u7fa9\u8fd4\u56de\u6309\u9215\u7684\u5716\u7247\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u4f7f\u7528UIBarButtonItem\u7684init\u65b9\u6cd5\u4f86\u8a2d\u7f6e\u5716\u7247\uff0c\u50cf\u9019\u6a23\uff1a<br \/>\n<code>let backButton = UIBarButtonItem(image: UIImage(named: \"back_icon\"), style: .plain, target: self, action: #selector(backButtonTapped))<\/code><\/p>\n<h3>Q2: \u53ef\u4ee5\u5728\u8fd4\u56de\u6309\u9215\u4e0a\u6dfb\u52a0\u52d5\u756b\u55ce\uff1f<\/h3>\n<p>A2: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728<code>backButtonTapped<\/code>\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u52d5\u756b\u6548\u679c\uff0c\u6bd4\u5982\u6de1\u5165\u6de1\u51fa\u6216\u7e2e\u653e\u6548\u679c\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u5728SwiftUI\u4e2d\u5be6\u73fe\u985e\u4f3c\u7684\u529f\u80fd\uff1f<\/h3>\n<p>A3: \u5728SwiftUI\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>NavigationLink<\/code>\u4f86\u5be6\u73fe\u8fd4\u56de\u529f\u80fd\uff0c\u4e26\u81ea\u5b9a\u7fa9\u6309\u9215\u6a23\u5f0f\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u7a0b\u5f0f\u8a9e\u6cd5\u81ea\u5b9a\u7fa9UIViewController\u8fd4\u56de\u6309\u9215\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u81ea\u5df1\u7684\u8a2d\u8a08\u98a8\u683c\u878d\u5165\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002<\/p>\n","protected":false},"author":1,"featured_media":6907,"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-6908","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-UIViewController-\u81ea\u5b9a\u7fa9\u8fd4\u56de\u6309\u94ae-\ud83d\udd19\ud83c\udfa8_1675398278.655298.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Nq","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6908","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=6908"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6908\/revisions"}],"predecessor-version":[{"id":13450,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6908\/revisions\/13450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6907"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}