{"id":7184,"date":"2025-05-30T13:21:44","date_gmt":"2025-06-05T05:33:44","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7184"},"modified":"2025-06-05T13:21:44","modified_gmt":"2025-06-05T05:33:44","slug":"https-badgameshow-com-steven-265","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-265\/","title":{"rendered":"Swift UIAlertController \u5b8c\u6574\u6559\u5b78\uff1a2025 \u6700\u65b0\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10 \ud83d\udea8"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UIAlertController, \u8b66\u544a\u8996\u7a97, iOS \u958b\u767c, Swift \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h1>Swift UIAlertController \u5b8c\u6574\u6559\u5b78\uff1a2025 \u6700\u65b0\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10 \ud83d\udea8<\/h1>\n<p>Swift \u662f Apple \u958b\u767c\u7684\u4e00\u7a2e\u958b\u653e\u6e90\u78bc\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u7528\u65bc iOS\u3001macOS\u3001watchOS \u548c tvOS \u61c9\u7528\u7a0b\u5f0f\u7684\u958b\u767c\u3002UIAlertController \u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u5728 iOS \u61c9\u7528\u4e2d\u65b9\u4fbf\u5730\u5275\u5efa\u8b66\u544a\u8996\u7a97\u6216\u64cd\u4f5c\u63d0\u793a\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f UIAlertController\uff1f<\/h2>\n<p>UIAlertController \u4e3b\u8981\u7528\u65bc\u5411\u7528\u6236\u986f\u793a\u8b66\u544a\u6d88\u606f\uff0c\u4e26\u63d0\u4f9b\u4e00\u500b\u6216\u591a\u500b\u6309\u9215\u4f9b\u7528\u6236\u9078\u64c7\u3002\u5b83\u53ef\u4ee5\u7528\u65bc\u63d0\u793a\u7528\u6236\u8f38\u5165\u4fe1\u606f\u3001\u63d0\u4f9b\u9078\u64c7\u6216\u57f7\u884c\u7279\u5b9a\u64cd\u4f5c\u3002<\/p>\n<h2>\u5982\u4f55\u5275\u5efa UIAlertController<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5275\u5efa UIAlertController \u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3>1. \u5275\u5efa UIAlertController \u7269\u4ef6<\/h3>\n<pre><code class=\"language-swift line-numbers\">let alertController = UIAlertController(title: \"\u6a19\u984c\", message: \"\u9019\u662f\u4e00\u5247\u6d88\u606f\", preferredStyle: .alert)<\/code><\/pre>\n<h3>2. \u6dfb\u52a0\u6309\u9215\u64cd\u4f5c<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u500b\u6216\u591a\u500b\u6309\u9215\u4f9b\u7528\u6236\u9078\u64c7\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let action1 = UIAlertAction(title: \"\u9078\u9805 1\", style: .default) { (action: UIAlertAction) in\n    print(\"\u9078\u9805 1 \u88ab\u9078\u64c7\")\n}\nlet action2 = UIAlertAction(title: \"\u9078\u9805 2\", style: .default) { (action: UIAlertAction) in\n    print(\"\u9078\u9805 2 \u88ab\u9078\u64c7\")\n}\nalertController.addAction(action1)\nalertController.addAction(action2)<\/code><\/pre>\n<h3>3. \u986f\u793a UIAlertController<\/h3>\n<p>\u6700\u5f8c\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u5c07 UIAlertController \u986f\u793a\u7d66\u7528\u6236\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">self.present(alertController, animated: true, completion: nil)<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u5728\u4f7f\u7528 UIAlertController \u6642\u9047\u5230\u554f\u984c\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd\u60a8\u5728\u4e3b\u7dda\u7a0b\u4e2d\u547c\u53eb <code>present<\/code> \u65b9\u6cd5\u3002<\/li>\n<li>\u6aa2\u67e5\u60a8\u7684\u7269\u4ef6\u662f\u5426\u5df2\u7d93\u6b63\u78ba\u521d\u59cb\u5316\u3002<\/li>\n<li>\u78ba\u4fdd <code>UIAlertAction<\/code> \u88ab\u6b63\u78ba\u6dfb\u52a0\u5230 <code>UIAlertController<\/code> \u4e2d\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UIAlertController \u4e0d\u50c5\u9650\u65bc\u986f\u793a\u8b66\u544a\u8996\u7a97\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u8f38\u5165\u6846\u7684\u5be6\u73fe\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5275\u5efa\u4e00\u500b\u5e36\u6709\u6587\u672c\u8f38\u5165\u6846\u7684\u8b66\u544a\u8996\u7a97\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">alertController.addTextField { (textField) in\n    textField.placeholder = \"\u8f38\u5165\u60a8\u7684\u540d\u7a31\"\n}\nlet submitAction = UIAlertAction(title: \"\u63d0\u4ea4\", style: .default) { (action) in\n    let textField = alertController.textFields![0]\n    print(\"\u7528\u6236\u8f38\u5165: \\(textField.text ?? \"\")\")\n}\nalertController.addAction(submitAction)<\/code><\/pre>\n<h2>\u7d50\u8ad6<\/h2>\n<p>UIAlertController \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u4f7f\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u5728 iOS \u61c9\u7528\u4e2d\u5275\u5efa\u8b66\u544a\u8996\u7a97\uff0c\u986f\u793a\u6d88\u606f\u548c\u9078\u9805\u3002\u96a8\u8457 Swift \u8a9e\u8a00\u7684\u9032\u6b65\uff0c\u4f7f\u7528 UIAlertController \u7684\u65b9\u6cd5\u4e5f\u5728\u4e0d\u65b7\u66f4\u65b0\uff0c\u78ba\u4fdd\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u7b26\u5408\u6700\u65b0\u7684\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UIAlertController\u8b66\u544a\u8996\u7a97\u4f7f\u7528-\ud83d\udea8_1675404284.327061.webp\" alt=\"Swift UIAlertController\u8b66\u544a\u8996\u7a97\u4f7f\u7528 \ud83d\udea8\" title=\"Swift UIAlertController, \u8b66\u544a\u8996\u7a97, \u4f7f\u7528\u65b9\u6cd5, \u6a19\u984c, \u6309\u9215\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728 UIAlertController \u4e2d\u6dfb\u52a0\u8f38\u5165\u6846\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>addTextField<\/code> \u65b9\u6cd5\u4f86\u6dfb\u52a0\u6587\u672c\u8f38\u5165\u6846\uff0c\u4e26\u5728\u6309\u9215\u64cd\u4f5c\u4e2d\u53d6\u5f97\u7528\u6236\u8f38\u5165\u7684\u503c\u3002<\/p>\n<h3>2. UIAlertController \u53ef\u4ee5\u986f\u793a\u54ea\u4e9b\u985e\u578b\u7684\u6309\u9215\uff1f<\/h3>\n<p>UIAlertController \u53ef\u4ee5\u986f\u793a <code>default<\/code>\u3001<code>cancel<\/code> \u548c <code>destructive<\/code> \u4e09\u7a2e\u98a8\u683c\u7684\u6309\u9215\u3002<\/p>\n<h3>3. \u5982\u4f55\u8655\u7406 UIAlertController \u7684\u6309\u9215\u9ede\u64ca\u4e8b\u4ef6\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5728\u5275\u5efa <code>UIAlertAction<\/code> \u6642\u6307\u5b9a\u4e00\u500b\u9589\u5305\uff0c\u8a72\u9589\u5305\u6703\u5728\u7528\u6236\u9ede\u64ca\u6309\u9215\u6642\u57f7\u884c\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):\u4e86\u89e3SwiftUIAlertController\u8b66\u544a\u8996\u7a97\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5b78\u7fd2\u5982\u4f55\u5728Swift\u4e2d\u4f7f\u7528UIAlertController\u8b66\u544a\u8996\u7a97\uff0c\u4ee5\u53ca\u5982\u4f55\u8a2d\u7f6e\u8b66\u544a\u8996\u7a97\u7684\u6a19\u984c\u3001\u6309\u9215\u3001\u6587\u5b57\u7b49\u5167\u5bb9\u3002<\/p>\n","protected":false},"author":1,"featured_media":7183,"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-7184","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-UIAlertController\u8b66\u544a\u8996\u7a97\u4f7f\u7528-\ud83d\udea8_1675404284.327061.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1RS","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7184","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=7184"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7184\/revisions"}],"predecessor-version":[{"id":13217,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7184\/revisions\/13217"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7183"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}