{"id":6904,"date":"2025-06-03T13:42:52","date_gmt":"2025-06-05T05:56:51","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6904"},"modified":"2025-06-05T13:42:52","modified_gmt":"2025-06-05T05:56:51","slug":"https-badgameshow-com-steven-128","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-128\/","title":{"rendered":"2025 \u5e74\u6700\u65b0 Swift \u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97\u6559\u5b78 \ud83d\udcac\ud83c\udf89"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97, Swift \u6559\u5b78, iOS \u958b\u767c\"><\/p>\n<h2>Swift \u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97\u6982\u8ff0<\/h2>\n<p>Swift \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc iOS \u548c macOS \u61c9\u7528\u7a0b\u5f0f\u7684\u958b\u767c\u3002\u5728 Swift \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97\uff0c\u4ee5\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u4f7f\u7528\u8005\u9ad4\u9a57\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6307\u5c0e\u60a8\u4f7f\u7528 <strong>UIAlertController<\/strong> \u985e\u5225\u4f86\u5275\u5efa\u548c\u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u4f5c\u7bc4\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5275\u5efa\u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97<\/h2>\n<p>\u70ba\u4e86\u5275\u5efa\u4e00\u500b\u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97\uff0c\u958b\u767c\u8005\u9700\u8981\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u9032\u884c\uff1a<\/p>\n<h3>1. \u5275\u5efa UIAlertController \u7269\u4ef6<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b <code>UIAlertController<\/code> \u7269\u4ef6\uff0c\u4e26\u6307\u5b9a\u5f48\u51fa\u8996\u7a97\u7684\u6a19\u984c\u548c\u6d88\u606f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let alertController = UIAlertController(title: \"Title\", message: \"Message\", preferredStyle: .alert)<\/code><\/pre>\n<h3>2. \u6dfb\u52a0\u6309\u9215\u52d5\u4f5c<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u6309\u9215\u5230\u5f48\u51fa\u8996\u7a97\u4e2d\uff0c\u4e26\u6307\u5b9a\u6309\u9215\u7684\u884c\u70ba\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let okAction = UIAlertAction(title: \"OK\", style: .default) { (action) in\n    \/\/ \u5728\u4f7f\u7528\u8005\u9ede\u64ca OK \u6309\u9215\u5f8c\u57f7\u884c\u7684\u4ee3\u78bc\n}\nalertController.addAction(okAction)<\/code><\/pre>\n<h3>3. \u6dfb\u52a0\u6587\u672c\u6846<\/h3>\n<p>\u5982\u679c\u9700\u8981\uff0c\u60a8\u9084\u53ef\u4ee5\u70ba\u5f48\u51fa\u8996\u7a97\u6dfb\u52a0\u6587\u672c\u6846\uff0c\u8b93\u4f7f\u7528\u8005\u8f38\u5165\u5167\u5bb9\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">alertController.addTextField { (textField) in\n    textField.placeholder = \"Enter something\"\n}<\/code><\/pre>\n<h3>4. \u986f\u793a\u5f48\u51fa\u8996\u7a97<\/h3>\n<p>\u6700\u5f8c\uff0c\u60a8\u53ef\u4ee5\u5c07\u5f48\u51fa\u8996\u7a97\u986f\u793a\u51fa\u4f86\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">present(alertController, animated: true, completion: nil)<\/code><\/pre>\n<h2>\u5b8c\u6574\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728 Swift \u4e2d\u5275\u5efa\u4e26\u986f\u793a\u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func showCustomAlert() {\n    let alertController = UIAlertController(title: \"Welcome\", message: \"Please enter your name\", preferredStyle: .alert)\n    \n    alertController.addTextField { (textField) in\n        textField.placeholder = \"Your name\"\n    }\n    \n    let okAction = UIAlertAction(title: \"OK\", style: .default) { (action) in\n        let name = alertController.textFields?.first?.text\n        print(\"Hello, \\(name ?? \"Guest\")!\")\n    }\n    \n    alertController.addAction(okAction)\n    present(alertController, animated: true, completion: nil)\n}<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 <code>UIAlertController<\/code> \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u672a\u5728\u4e3b\u7dda\u7a0b\u4e2d\u66f4\u65b0 UI\uff1a<\/strong>\u78ba\u4fdd\u5728\u4e3b\u7dda\u7a0b\u4e2d\u986f\u793a\u5f48\u51fa\u8996\u7a97\uff0c\u4ee5\u907f\u514d UI \u932f\u8aa4\u3002<\/li>\n<li><strong>\u672a\u6dfb\u52a0\u5fc5\u8981\u7684\u6309\u9215\uff1a<\/strong>\u78ba\u4fdd\u6bcf\u500b\u5f48\u51fa\u8996\u7a97\u81f3\u5c11\u6709\u4e00\u500b\u884c\u52d5\u6309\u9215\uff0c\u5426\u5247\u4f7f\u7528\u8005\u7121\u6cd5\u95dc\u9589\u5f48\u51fa\u8996\u7a97\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u64f4\u5c55\u5f48\u51fa\u8996\u7a97\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u6dfb\u52a0\u591a\u500b\u6587\u672c\u6846\u3001\u5716\u50cf\u6216\u81ea\u5b9a\u7fa9\u98a8\u683c\uff0c\u4ee5\u589e\u5f37\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97-\ud83d\udcac\ud83c\udf89_1675398046.837972.webp\" alt=\"Swift \u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97 \ud83d\udcac\ud83c\udf89\" title=\"Swift \u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97\u3001Swift \u5f48\u51fa\u8996\u7a97\u3001Swift \u81ea\u5b9a\u7fa9\u8996\u7a97\u3001Swift \u8996\u7a97\u8a2d\u8a08\u3001Swift \u8996\u7a97\u7a0b\u5f0f\u78bc\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: UIAlertController \u53ef\u4ee5\u81ea\u5b9a\u7fa9\u6a23\u5f0f\u55ce\uff1f<\/h3>\n<p>A1: UIAlertController \u63d0\u4f9b\u7684\u6a23\u5f0f\u6709\u9650\uff0c\u4e0d\u904e\u53ef\u4ee5\u900f\u904e\u81ea\u5b9a\u7fa9\u8996\u5716\u4f86\u5be6\u73fe\u66f4\u8c50\u5bcc\u7684\u8a2d\u8a08\u9700\u6c42\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5728\u5f48\u51fa\u8996\u7a97\u4e2d\u6dfb\u52a0\u5716\u7247\uff1f<\/h3>\n<p>A2: \u60a8\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u4e00\u500b UIViewController\uff0c\u7136\u5f8c\u5c07\u5176\u5d4c\u5165\u5230 <code>UIAlertController<\/code> \u4e2d\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5728\u5f48\u51fa\u8996\u7a97\u4e2d\u986f\u793a\u5716\u7247\u4e86\u3002<\/p>\n<h3>Q3: \u70ba\u4ec0\u9ebc\u5f48\u51fa\u8996\u7a97\u6c92\u6709\u986f\u793a\uff1f<\/h3>\n<p>A3: \u78ba\u4fdd\u5728\u4e3b\u61c9\u7528\u7a0b\u5f0f\u8996\u5716\u63a7\u5236\u5668\u4e2d\u547c\u53eb <code>present(_:animated:completion:)<\/code> \u65b9\u6cd5\uff0c\u4e26\u4e14\u6aa2\u67e5\u662f\u5426\u6709\u4efb\u4f55\u8996\u5716\u63a7\u5236\u5668\u6b63\u5728\u5448\u73fe\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97\uff0c\u4e26\u63d0\u4f9b\u7c21\u55ae\u6613\u61c2\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\uff0c\u8f15\u9b06\u5be6\u73fe\u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":6903,"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-6904","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-\u81ea\u5b9a\u7fa9\u5f48\u51fa\u8996\u7a97-\ud83d\udcac\ud83c\udf89_1675398046.837972.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Nm","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6904","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=6904"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6904\/revisions"}],"predecessor-version":[{"id":13469,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6904\/revisions\/13469"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6903"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}