{"id":6737,"date":"2025-06-03T12:39:37","date_gmt":"2025-06-05T05:10:36","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6737"},"modified":"2025-06-05T12:39:37","modified_gmt":"2025-06-05T05:10:36","slug":"https-badgameshow-com-steven-36","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-36\/","title":{"rendered":"Swift \u7a0b\u5f0f\u8a2d\u7f6e\u5206\u9801\u756b\u9762 (UIPageViewController) &#8211; 2025 \u6700\u65b0\u6559\u5b78"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UIPageViewController, iOS \u958b\u767c, Swift \u6559\u5b78\"><\/p>\n<h2>\u4ec0\u9ebc\u662f UIPageViewController\uff1f<\/h2>\n<p>Swift \u662f\u7531 Apple \u958b\u767c\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc iOS\u3001macOS\u3001watchOS \u548c tvOS \u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u8a2d\u7f6e\u5206\u9801\u756b\u9762\uff08UIPageViewController\uff09\uff0c\u9019\u662f\u4e00\u7a2e\u6d41\u884c\u7684\u8996\u5716\u63a7\u5236\u5668\uff0c\u80fd\u5920\u8b93\u958b\u767c\u8005\u8f15\u9b06\u5730\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u5275\u5efa\u591a\u9801\u5167\u5bb9\u3002<\/p>\n<h2>\u8a2d\u7f6e UIPageViewController \u7684\u6b65\u9a5f<\/h2>\n<p>\u8981\u5728 Xcode \u4e2d\u8a2d\u7f6e UIPageViewController\uff0c\u8acb\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u9032\u884c\uff1a<\/p>\n<ol>\n<li>\u5728 Xcode \u4e2d\u5275\u5efa\u4e00\u500b\u65b0\u7684 iOS \u61c9\u7528\u7a0b\u5f0f\u5c08\u6848\u3002<\/li>\n<li>\u5728\u5c08\u6848\u4e2d\u6dfb\u52a0\u4e00\u500b\u65b0\u7684 UIPageViewController \u7269\u4ef6\uff0c\u4e26\u5c07\u5176\u5d4c\u5165\u5230\u61c9\u7528\u7a0b\u5f0f\u7684\u4e3b\u8996\u5716\u63a7\u5236\u5668\u4e2d\u3002<\/li>\n<li>\u5275\u5efa\u8981\u986f\u793a\u7684 UIViewController \u7269\u4ef6\uff0c\u4e26\u5c07\u5b83\u5011\u6dfb\u52a0\u5230 UIPageViewController\u3002<\/li>\n<\/ol>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b<\/h2>\n<p>\u4e0b\u9762\u7684 Swift \u4ee3\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u8a2d\u7f6e UIPageViewController\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import UIKit\n\nclass MyPageViewController: UIPageViewController, UIPageViewControllerDataSource {\n\n    var pages: [UIViewController] = []\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        self.dataSource = self\n\n        \/\/ \u521d\u59cb\u5316\u9801\u9762\n        let page1 = UIViewController()\n        let page2 = UIViewController()\n        pages = [page1, page2]\n\n        \/\/ \u8a2d\u5b9a\u521d\u59cb\u986f\u793a\u7684\u9801\u9762\n        setViewControllers([pages[0]], direction: .forward, animated: true, completion: nil)\n    }\n\n    \/\/ MARK: - UIPageViewControllerDataSource\n    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {\n        guard let currentIndex = pages.firstIndex(of: viewController), currentIndex > 0 else {\n            return nil\n        }\n        return pages[currentIndex - 1]\n    }\n\n    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {\n        guard let currentIndex = pages.firstIndex(of: viewController), currentIndex < pages.count - 1 else {\n            return nil\n        }\n        return pages[currentIndex + 1]\n    }\n}<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u8a2d\u7f6e UIPageViewController \u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<ul>\n<li><strong>\u9801\u9762\u672a\u986f\u793a\uff1a<\/strong>\u8acb\u78ba\u8a8d\u5df2\u6b63\u78ba\u8a2d\u7f6e\u8cc7\u6599\u6e90\uff0c\u4e14\u9801\u9762\u6578\u7d44\u4e0d\u70ba\u7a7a\u3002<\/li>\n<li><strong>\u6ed1\u52d5\u6548\u679c\u4e0d\u6d41\u66a2\uff1a<\/strong>\u6aa2\u67e5\u662f\u5426\u6709\u904e\u591a\u7684\u8996\u5716\u91cd\u7e6a\uff0c\u5efa\u8b70\u5728 viewDidLoad \u4e2d\u9032\u884c\u521d\u59cb\u5316\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UIPageViewController \u53ef\u4ee5\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u5834\u666f\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u96fb\u5b50\u66f8\u61c9\u7528\uff1a\u8b93\u7528\u6236\u80fd\u5920\u8f15\u9b06\u7ffb\u9801\u3002<\/li>\n<li>\u7522\u54c1\u5c55\u793a\uff1a\u5c55\u793a\u591a\u500b\u7522\u54c1\u7684\u5716\u7247\u6216\u8cc7\u8a0a\u3002<\/li>\n<li>\u6559\u5b78\u61c9\u7528\uff1a\u5206\u6b65\u9a5f\u6559\u5b78\uff0c\u9010\u6b65\u5f15\u5c0e\u7528\u6236\u5b8c\u6210\u4efb\u52d9\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u8a2d\u7f6e\u5206\u9801\u756b\u9762-UIPageViewController-\ud83d\udcbb_1675394840.310472.webp\" alt=\"Swift \u8a2d\u7f6e\u5206\u9801\u756b\u9762 (UIPageViewController) \ud83d\udcbb\" title=\"Swift \u8a2d\u7f6e\u5206\u9801\u756b\u9762\u3001UIPageViewController\u3001Swift \u65b9\u6cd5\u3001Swift \u5206\u9801\u756b\u9762\u3001Swift \u5206\u9801\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UIPageViewController \u53ef\u4ee5\u986f\u793a\u591a\u5c11\u9801\u9762\uff1f<\/h3>\n<p>\u7406\u8ad6\u4e0a\uff0c\u60a8\u53ef\u4ee5\u986f\u793a\u7121\u9650\u9801\u9762\uff0c\u53ea\u8981\u5728 pages \u9663\u5217\u4e2d\u6dfb\u52a0\u66f4\u591a UIViewController \u5be6\u4f8b\u5373\u53ef\u3002<\/p>\n<h3>2. \u5982\u4f55\u5728 UIPageViewController \u4e4b\u9593\u50b3\u905e\u6578\u64da\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u901a\u904e\u521d\u59cb\u5316 UIViewController \u6642\u50b3\u905e\u6578\u64da\uff0c\u6216\u8005\u4f7f\u7528 delegate \u6a21\u5f0f\u4f86\u5be6\u73fe\u9801\u9762\u4e4b\u9593\u7684\u6578\u64da\u50b3\u905e\u3002<\/p>\n<h3>3. \u5982\u4f55\u81ea\u5b9a\u7fa9 UIPageViewController \u7684\u904e\u6e21\u52d5\u756b\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u7fa9\u8f49\u5834\u63a7\u5236\u5668\u4f86\u5be6\u73fe\u4e0d\u540c\u7684\u904e\u6e21\u6548\u679c\uff0c\u9019\u9700\u8981\u9075\u5faa UINavigationControllerDelegate \u548c UIViewControllerTransitioningDelegate \u5354\u8b70\u3002<\/p>\n<p>```<br \/>\n---<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u8a2d\u7f6eUIPageViewController\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5efa\u7acb\u5206\u9801\u756b\u9762\uff0c\u4e26\u4e14\u66f4\u52a0\u4e86\u89e3Swift\u4e2d\u7684methods()\u51fd\u6578\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":6736,"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-6737","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-\u8a2d\u7f6e\u5206\u9801\u756b\u9762-UIPageViewController-\ud83d\udcbb_1675394840.310472.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1KF","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6737","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=6737"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6737\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6736"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}