{"id":6662,"date":"2025-05-29T11:36:20","date_gmt":"2025-06-05T04:36:19","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/uncategorized\/https-badgameshow-com-steven-9\/"},"modified":"2025-06-05T11:36:20","modified_gmt":"2025-06-05T04:36:19","slug":"https-badgameshow-com-steven-9","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-9\/","title":{"rendered":"Swift \u5f48\u7a97\u8996\u7a97\uff08UIAlertController\uff09\ud83d\udce3\uff1a2025 \u6700\u65b0\u5be6\u73fe\u65b9\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UIAlertController, Swift \u7a0b\u5f0f\u6559\u5b78, \u5f48\u51fa\u5f0f\u8996\u7a97\"><\/p>\n<p>Swift \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u80fd\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u529f\u80fd\u5f37\u5927\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u5728 Swift \u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 <code>UIAlertController<\/code> \u4f86\u5be6\u73fe\u5f48\u51fa\u5f0f\u8996\u7a97\uff0c\u8b93\u4f7f\u7528\u8005\u66f4\u8f15\u9b06\u5730\u8207\u61c9\u7528\u7a0b\u5f0f\u4e92\u52d5\u3002\u672c\u7bc7\u6587\u7ae0\u5c07\u63d0\u4f9b\u6700\u5b8c\u6574\u7684\u6559\u5b78\u6d41\u7a0b\uff0c\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u4ee5\u53ca\u932f\u8aa4\u6392\u9664\u7684\u6280\u5de7\uff0c\u5e6b\u52a9\u4f60\u719f\u6089 <code>UIAlertController<\/code> \u7684\u4f7f\u7528\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f UIAlertController\uff1f<\/h2>\n<p><code>UIAlertController<\/code> \u662f\u4e00\u500b Swift \u7684\u5167\u5efa\u985e\u5225\uff0c\u7528\u65bc\u5feb\u901f\u5efa\u7acb\u5f48\u51fa\u5f0f\u8996\u7a97\uff0c\u8b93\u4f7f\u7528\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u8207\u61c9\u7528\u7a0b\u5f0f\u4e92\u52d5\u3002\u958b\u767c\u8005\u53ef\u4ee5\u8a2d\u5b9a\u5f48\u51fa\u5f0f\u8996\u7a97\u7684\u6a19\u984c\u3001\u8a0a\u606f\u3001\u6309\u9215\u53ca\u5176\u52d5\u4f5c\uff0c\u8b93\u4f7f\u7528\u8005\u900f\u904e\u7c21\u55ae\u7684\u65b9\u5f0f\u56de\u61c9\u61c9\u7528\u7a0b\u5f0f\u7684\u9700\u6c42\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 UIAlertController\uff1f<\/h2>\n<p>\u4f7f\u7528 <code>UIAlertController<\/code> \u4f86\u5efa\u7acb\u5f48\u51fa\u5f0f\u8996\u7a97\u975e\u5e38\u7c21\u55ae\uff0c\u4ee5\u4e0b\u662f\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u5efa\u7acb\u4e00\u500b <code>UIAlertController<\/code> \u7269\u4ef6\uff0c\u4e26\u8a2d\u5b9a\u6a19\u984c\u3001\u8a0a\u606f\u3001\u6309\u9215\u53ca\u6309\u9215\u7684\u52d5\u4f5c\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-swift line-numbers\">let alertController = UIAlertController(title: \"\u6a19\u984c\", message: \"\u8a0a\u606f\", preferredStyle: .alert)\nlet okAction = UIAlertAction(title: \"OK\", style: .default, handler: nil)\nalertController.addAction(okAction)\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5c07 <code>UIAlertController<\/code> \u7269\u4ef6\u986f\u793a\u51fa\u4f86\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-swift line-numbers\">present(alertController, animated: true, completion: nil)\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5728\u6309\u9215\u7684\u52d5\u4f5c\u4e2d\u52a0\u5165\u984d\u5916\u7684\u7a0b\u5f0f\u78bc\uff0c\u8b93\u4f7f\u7528\u8005\u6309\u4e0b\u6309\u9215\u5f8c\uff0c\u57f7\u884c\u7279\u5b9a\u7684\u52d5\u4f5c\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-swift line-numbers\">let okAction = UIAlertAction(title: \"OK\", style: .default) { (action) in\n    \/\/ \u5728\u9019\u88e1\u52a0\u5165\u984d\u5916\u7684\u7a0b\u5f0f\u78bc\n}\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 <code>UIAlertController<\/code> \u4f86\u5efa\u7acb\u5f48\u51fa\u5f0f\u8996\u7a97\u7684\u7c21\u55ae\u6b65\u9a5f\u3002\u958b\u767c\u8005\u53ef\u4ee5\u85c9\u7531\u9019\u500b\u5de5\u5177\u8b93\u4f7f\u7528\u8005\u66f4\u8f15\u9b06\u5730\u8207\u61c9\u7528\u7a0b\u5f0f\u4e92\u52d5\u3002<\/p>\n<h2>\u5e38\u898b\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 <code>UIAlertController<\/code> \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u672a\u80fd\u986f\u793a\u5f48\u7a97\uff1a<\/strong>\u78ba\u4fdd\u5728\u4e3b\u7dda\u7a0b\u4e2d\u547c\u53eb <code>present<\/code> \u65b9\u6cd5\u3002<\/li>\n<li><strong>\u6309\u9215\u672a\u53cd\u61c9\uff1a<\/strong>\u78ba\u8a8d\u662f\u5426\u6b63\u78ba\u8a2d\u5b9a\u4e86 <code>UIAlertAction<\/code> \u7684 handler\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4f60\u53ef\u4ee5\u5c07 <code>UIAlertController<\/code> \u8207 <code>UITextField<\/code> \u7d50\u5408\u4f7f\u7528\uff0c\u8b93\u4f7f\u7528\u8005\u8f38\u5165\u8cc7\u6599\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let alertController = UIAlertController(title: \"\u8f38\u5165\", message: \"\u8acb\u8f38\u5165\u4f60\u7684\u540d\u5b57\", preferredStyle: .alert)\nalertController.addTextField { (textField) in\n    textField.placeholder = \"\u540d\u5b57\"\n}\nlet submitAction = UIAlertAction(title: \"\u63d0\u4ea4\", style: .default) { (action) in\n    let name = alertController.textFields?.first?.text\n    print(\"\u4f7f\u7528\u8005\u8f38\u5165\u7684\u540d\u5b57\u662f\uff1a\\(name ?? \"\")\")\n}\nalertController.addAction(submitAction)\npresent(alertController, animated: true, completion: nil)\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5f48\u7a97\u8996\u7a97-\uff08UIAlertController\uff09\ud83d\udce3-\u5be6\u73fe\u5f48\u51fa\u5f0f\u8996\u7a97_1675393333.897856.webp\" \nalt=\"Swift \u5f48\u7a97\u8996\u7a97 \uff08UIAlertController\uff09\ud83d\udce3 \u5be6\u73fe\u5f48\u51fa\u5f0f\u8996\u7a97\" \ntitle=\"Swift \u5f48\u7a97\u8996\u7a97, UIAlertController, \u5f48\u51fa\u5f0f\u8996\u7a97, Swift methods(), Swift\u529f\u80fd\" \nwidth=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. UIAlertController \u53ef\u4ee5\u7528\u4f86\u986f\u793a\u54ea\u4e9b\u985e\u578b\u7684\u5167\u5bb9\uff1f<\/h3>\n<p>UIAlertController \u53ef\u4ee5\u986f\u793a\u8b66\u544a\u3001\u63d0\u793a\u3001\u8f38\u5165\u6846\u7b49\u591a\u7a2e\u5167\u5bb9\uff0c\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684 <code>preferredStyle<\/code>\u3002<\/p>\n<h3>2. \u5982\u4f55\u5728 UIAlertController \u4e2d\u6dfb\u52a0\u591a\u500b\u6309\u9215\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u901a\u904e\u591a\u6b21\u8abf\u7528 <code>addAction<\/code> \u65b9\u6cd5\u4f86\u6dfb\u52a0\u591a\u500b\u6309\u9215\uff0c\u6bcf\u500b\u6309\u9215\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u52d5\u4f5c\u3002<\/p>\n<h3>3. UIAlertController \u662f\u5426\u652f\u6301\u81ea\u5b9a\u7fa9\u6a23\u5f0f\uff1f<\/h3>\n<p>UIAlertController \u7684\u6a23\u5f0f\u662f\u56fa\u5b9a\u7684\uff0c\u82e5\u9700\u8981\u81ea\u5b9a\u7fa9\u5916\u89c0\uff0c\u5efa\u8b70\u4f7f\u7528\u81ea\u5b9a\u7fa9\u7684 UIViewController \u4f86\u5be6\u73fe\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u66f4\u6df1\u5165\u4e86\u89e3\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 <code>UIAlertController<\/code> \u4f86\u5be6\u73fe\u5f48\u7a97\u8996\u7a97\u529f\u80fd\uff01<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684UIAlertController\u4f86\u5be6\u73fe\u5f48\u51fa\u5f0f\u8996\u7a97\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684methods()\u51fd\u6578\u4f86\u5b8c\u6210\u5f48\u51fa\u5f0f\u8996\u7a97\u7684\u529f\u80fd\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u5728Swift\u4e2d\u5be6\u73fe\u5f48\u51fa\u5f0f\u8996\u7a97\u3002<\/p>\n","protected":false},"author":1,"featured_media":6661,"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-6662","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-\u5f48\u7a97\u8996\u7a97-\uff08UIAlertController\uff09\ud83d\udce3-\u5be6\u73fe\u5f48\u51fa\u5f0f\u8996\u7a97_1675393333.897856.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Js","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6662","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=6662"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6662\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6661"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}