{"id":6121,"date":"2024-06-13T14:49:13","date_gmt":"2024-06-13T06:49:13","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6121"},"modified":"2024-06-13T14:49:13","modified_gmt":"2024-06-13T06:49:13","slug":"swiftui%e5%b0%91%e4%ba%86-uiviewcontroller%e5%8e%9f%e6%9d%a5%e6%8e%a7%e5%88%b6%e5%99%a8%e7%9a%84%e9%80%bb%e8%be%91%e5%ba%94%e8%af%a5%e5%86%99%e5%9c%a8%e5%93%aa%e9%87%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6121","title":{"rendered":"[SwiftUI]\u5c11\u4e86 UIViewController,\u539f\u6765\u63a7\u5236\u5668\u7684\u903b\u8f91\u5e94\u8be5\u5199\u5728\u54ea\u91cc\uff1f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u975e\u539f\u521b\uff0c\u6765\u81ea Gemini \u95ee\u7b54<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 SwiftUI \u4e2d\uff0c\u7531\u4e8e\u4e0d\u518d\u4f7f\u7528\u4f20\u7edf\u7684 UIViewController \u5c42\u7ea7\u7ed3\u6784\uff0c\u56e0\u6b64\u539f\u6765\u7531\u63a7\u5236\u5668\u8d1f\u8d23\u7684\u903b\u8f91\u9700\u8981\u5206\u6563\u5230\u4e0d\u540c\u7684\u89c6\u56fe\u548c\u6a21\u578b\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8bae\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. \u5c06\u89c6\u56fe\u903b\u8f91\u8fc1\u79fb\u5230\u89c6\u56fe\u4e2d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 SwiftUI \u4e2d\uff0c\u89c6\u56fe\u8d1f\u8d23\u5448\u73b0 UI \u5e76\u5904\u7406\u7528\u6237\u4ea4\u4e92\u3002\u56e0\u6b64\uff0c\u539f\u6765\u7531\u63a7\u5236\u5668\u8d1f\u8d23\u7684\u4e0e UI \u76f8\u5173\u7684\u903b\u8f91\uff0c\u4f8b\u5982\u8bbe\u7f6e\u5bfc\u822a\u680f\u3001\u5904\u7406\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u7b49\uff0c\u5e94\u8be5\u8fc1\u79fb\u5230\u76f8\u5e94\u7684\u89c6\u56fe\u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u5728\u4f20\u7edf\u7684 UIKit \u4e2d\uff0c\u8bbe\u7f6e\u5bfc\u822a\u680f\u6807\u9898\u901a\u5e38\u9700\u8981\u5728 viewDidLoad \u65b9\u6cd5\u4e2d\u5b8c\u6210\u3002\u800c\u5728 SwiftUI \u4e2d\uff0c\u5219\u53ef\u4ee5\u76f4\u63a5\u5728 NavigationView \u7684 navigationTitle \u5c5e\u6027\u4e2d\u8bbe\u7f6e\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Swift<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NavigationView {\n  Text(\"Hello, World!\")\n    .navigationTitle(\"My App\")\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. \u5c06\u6570\u636e\u6a21\u578b\u903b\u8f91\u8fc1\u79fb\u5230\u6a21\u578b\u4e2d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 SwiftUI \u4e2d\uff0c\u6570\u636e\u6a21\u578b\u8d1f\u8d23\u5b58\u50a8\u548c\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u6570\u636e\u3002\u56e0\u6b64\uff0c\u539f\u6765\u7531\u63a7\u5236\u5668\u8d1f\u8d23\u7684\u6570\u636e\u5904\u7406\u903b\u8f91\uff0c\u4f8b\u5982\u52a0\u8f7d\u6570\u636e\u3001\u4fdd\u5b58\u6570\u636e\u7b49\uff0c\u5e94\u8be5\u8fc1\u79fb\u5230\u76f8\u5e94\u7684\u6a21\u578b\u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u5728\u4f20\u7edf\u7684 UIKit \u4e2d\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528 UITableView \u6765\u663e\u793a\u6570\u636e\u3002\u5728\u52a0\u8f7d\u6570\u636e\u65f6\uff0c\u63a7\u5236\u5668\u9700\u8981\u8d1f\u8d23\u4ece\u7f51\u7edc\u6216\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u5e76\u5c06\u5176\u586b\u5145\u5230 UITableView \u7684\u6570\u636e\u6e90\u4e2d\u3002\u800c\u5728 SwiftUI \u4e2d\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 List \u89c6\u56fe\u6765\u663e\u793a\u6570\u636e\u3002\u6570\u636e\u6a21\u578b\u53ef\u4ee5\u8d1f\u8d23\u4ece\u7f51\u7edc\u6216\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u7b26\u5408 List \u89c6\u56fe\u6570\u636e\u6e90\u8981\u6c42\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. \u5c06\u72b6\u6001\u7ba1\u7406\u903b\u8f91\u8fc1\u79fb\u5230 @State \u6216 @ObservedObject \u4e2d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 SwiftUI \u4e2d\uff0c@State \u548c @ObservedObject \u7528\u4e8e\u7ba1\u7406\u89c6\u56fe\u7684\u72b6\u6001\u3002\u56e0\u6b64\uff0c\u539f\u6765\u7531\u63a7\u5236\u5668\u8d1f\u8d23\u7684\u72b6\u6001\u7ba1\u7406\u903b\u8f91\uff0c\u4f8b\u5982\u8ddf\u8e2a\u7528\u6237\u8f93\u5165\u3001\u66f4\u65b0 UI \u7b49\uff0c\u5e94\u8be5\u8fc1\u79fb\u5230 @State \u6216 @ObservedObject \u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u5728\u4f20\u7edf\u7684 UIKit \u4e2d\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528 UITextField \u6765\u8ba9\u7528\u6237\u8f93\u5165\u6587\u672c\u3002\u63a7\u5236\u5668\u9700\u8981\u8d1f\u8d23\u76d1\u542c UITextField \u7684\u6587\u672c\u53d8\u5316\u4e8b\u4ef6\uff0c\u5e76\u66f4\u65b0 UI\u3002\u800c\u5728 SwiftUI \u4e2d\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 TextField \u89c6\u56fe\u3002TextField \u89c6\u56fe\u4f1a\u81ea\u52a8\u66b4\u9732\u6587\u672c\u5c5e\u6027\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u89c6\u56fe\u4e2d\u4f7f\u7528 @State \u6216 @ObservedObject \u6765\u7ba1\u7406\u6587\u672c\u72b6\u6001\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4. \u5c06\u8def\u7531\u903b\u8f91\u8fc1\u79fb\u5230 SceneDelegate \u6216 App \u89c6\u56fe\u4e2d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 SwiftUI \u4e2d\uff0cSceneDelegate \u6216 App \u89c6\u56fe\u8d1f\u8d23\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u8def\u7531\u3002\u56e0\u6b64\uff0c\u539f\u6765\u7531\u63a7\u5236\u5668\u8d1f\u8d23\u7684\u8def\u7531\u903b\u8f91\uff0c\u4f8b\u5982\u5207\u6362\u89c6\u56fe\u7b49\uff0c\u5e94\u8be5\u8fc1\u79fb\u5230 SceneDelegate \u6216 App \u89c6\u56fe\u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c\u5728\u4f20\u7edf\u7684 UIKit \u4e2d\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528 pushViewController \u65b9\u6cd5\u6765\u5207\u6362\u5230\u53e6\u4e00\u4e2a\u89c6\u56fe\u63a7\u5236\u5668\u3002\u800c\u5728 SwiftUI \u4e2d\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 NavigationLink \u89c6\u56fe\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002NavigationLink \u89c6\u56fe\u53ef\u4ee5\u5c06\u7528\u6237\u4ece\u4e00\u4e2a\u89c6\u56fe\u5bfc\u822a\u5230\u53e6\u4e00\u4e2a\u89c6\u56fe\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u603b\u800c\u8a00\u4e4b\uff0c\u5728 SwiftUI \u4e2d\uff0c\u7531\u4e8e\u4e0d\u518d\u4f7f\u7528\u4f20\u7edf\u7684 UIViewController \u5c42\u7ea7\u7ed3\u6784\uff0c\u56e0\u6b64\u539f\u6765\u7531\u63a7\u5236\u5668\u8d1f\u8d23\u7684\u903b\u8f91\u9700\u8981\u5206\u6563\u5230\u4e0d\u540c\u7684\u89c6\u56fe\u548c\u6a21\u578b\u4e2d\u3002\u901a\u8fc7\u5c06\u903b\u8f91\u5206\u6563\u5230\u4e0d\u540c\u7684\u89d2\u8272\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u6613\u61c2\uff0c\u5e76\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u4e00\u4e9b\u989d\u5916\u7684\u5efa\u8bae\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9075\u5faa MVVM \u67b6\u6784\uff1a<\/strong> MVVM \u67b6\u6784\u662f\u4e00\u79cd\u5e38\u7528\u7684\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5c06\u89c6\u56fe\u3001\u6a21\u578b\u548c\u89c6\u56fe\u6a21\u578b\u5206\u79bb\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u6613\u61c2\u3002\u5728 SwiftUI \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 MVVM \u67b6\u6784\u6765\u7ec4\u7ec7\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 Combine \u6846\u67b6\uff1a<\/strong> Combine \u6846\u67b6\u662f\u4e00\u79cd\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u6570\u636e\u7684\u6846\u67b6\u3002\u5728 SwiftUI \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 Combine \u6846\u67b6\u6765\u7ba1\u7406\u89c6\u56fe\u7684\u72b6\u6001\u548c\u6570\u636e\u6d41\u3002<\/li>\n\n\n\n<li><strong>\u5b66\u4e60 SwiftUI \u7684\u6700\u65b0\u7279\u6027\uff1a<\/strong> SwiftUI \u4ecd\u5728\u5feb\u901f\u53d1\u5c55\u4e2d\uff0c\u4e0d\u65ad\u6dfb\u52a0\u65b0\u7684\u7279\u6027\u548c\u529f\u80fd\u3002\u5efa\u8bae\u5173\u6ce8 SwiftUI \u7684\u6700\u65b0\u53d1\u5c55\uff0c\u5e76\u5b66\u4e60\u65b0\u7684\u7279\u6027\u548c\u529f\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e0c\u671b\u8fd9\u4e9b\u4fe1\u606f\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\uff0c\u6765\u81ea Gemini \u95ee\u7b54 \u5728 SwiftUI \u4e2d\uff0c\u7531\u4e8e\u4e0d\u518d\u4f7f\u7528\u4f20\u7edf\u7684 UIViewController&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6121\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[66,67,65],"class_list":["post-6121","post","type-post","status-publish","format-standard","hentry","category-5","tag-ios","tag-swift","tag-swiftui"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6121"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6121\/revisions"}],"predecessor-version":[{"id":6122,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6121\/revisions\/6122"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}