{"id":367,"date":"2025-06-02T10:35:39","date_gmt":"2025-06-05T03:22:39","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=367"},"modified":"2025-06-05T10:35:39","modified_gmt":"2025-06-05T03:22:39","slug":"%e6%95%99%e5%ad%b8-swift-alert-%e7%af%84%e4%be%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e6%95%99%e5%ad%b8-swift-alert-%e7%af%84%e4%be%8b\/","title":{"rendered":"2025 \u6700\u65b0 Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u4f7f\u7528 UIAlertController \u5efa\u7acb\u63d0\u793a\u6846\u7bc4\u4f8b"},"content":{"rendered":"<p>## Swift \u63d0\u793a\u6846 (UIAlertController) \u7684\u4f7f\u7528\u6559\u5b78<\/p>\n<p>\u5728 Swift \u7a0b\u5f0f\u8a2d\u8a08\u4e2d\uff0c\u63d0\u793a\u6846\u662f\u4e00\u500b\u5e38\u898b\u7684 UI \u5143\u4ef6\uff0c\u901a\u5e38\u7528\u65bc\u8207\u4f7f\u7528\u8005\u9032\u884c\u4e92\u52d5\uff0c\u4f8b\u5982\u8981\u6c42\u8f38\u5165\u6216\u78ba\u8a8d\u67d0\u9805\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 Swift \u4e2d\u5efa\u7acb\u4e00\u500b\u57fa\u672c\u7684\u63d0\u793a\u6846\u7bc4\u4f8b\uff0c\u4e26\u9644\u4e0a\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>### \u5efa\u7acb\u63d0\u793a\u6846\u7684\u7bc4\u4f8b<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5c07\u5efa\u7acb\u4e00\u500b\u5305\u542b\u78ba\u8a8d\u6309\u9215\u7684\u63d0\u793a\u6846\uff0c\u63d0\u793a\u4f7f\u7528\u8005\u8f38\u5165\u50f9\u683c\u6216\u6578\u91cf\u3002<\/p>\n<p>&#8220;`swift<br \/>\n\/\/ \u5efa\u7acb\u4e00\u500b\u63d0\u793a\u6846<br \/>\nlet alertController = UIAlertController(<br \/>\n    title: &#8220;\u63d0\u793a&#8221;,<br \/>\n    message: &#8220;\u8acb\u8f38\u5165\u50f9\u683c\u6216\u6578\u91cf&#8221;,<br \/>\n    preferredStyle: .alert<br \/>\n)<\/p>\n<p>\/\/ \u5efa\u7acb[\u78ba\u8a8d]\u6309\u9215<br \/>\nlet okAction = UIAlertAction(<br \/>\n    title: &#8220;\u78ba\u8a8d&#8221;,<br \/>\n    style: .default,<br \/>\n    handler: { (action: UIAlertAction!) in<br \/>\n        print(&#8220;\u6309\u4e0b\u78ba\u8a8d\u5f8c\u7684\u52d5\u4f5c&#8221;)<br \/>\n    }<br \/>\n)<br \/>\nalertController.addAction(okAction)<\/p>\n<p>\/\/ \u986f\u793a\u63d0\u793a\u6846<br \/>\nself.present(alertController, animated: true, completion: nil)<br \/>\n&#8220;`<\/p>\n<p>### \u7a0b\u5f0f\u78bc\u89e3\u8aaa<\/p>\n<p>1. **UIAlertController**: \u9019\u662f\u4e00\u500b\u7528\u65bc\u986f\u793a\u8b66\u544a\u548c\u63d0\u793a\u6846\u7684\u63a7\u5236\u5668\u3002\u9019\u88e1\u6211\u5011\u6307\u5b9a\u4e86 `preferredStyle` \u70ba `.alert`\uff0c\u8868\u793a\u6211\u5011\u60f3\u8981\u4e00\u500b\u8b66\u544a\u6a23\u5f0f\u7684\u63d0\u793a\u6846\u3002<\/p>\n<p>2. **UIAlertAction**: \u9019\u662f\u63d0\u793a\u6846\u4e2d\u7684\u6309\u9215\u3002\u9019\u88e1\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba\u300c\u78ba\u8a8d\u300d\u7684\u6309\u9215\uff0c\u4e26\u8a2d\u7f6e\u4e86\u4e00\u500b\u8655\u7406\u51fd\u6578\uff0c\u7576\u6309\u9215\u88ab\u9ede\u64ca\u6642\uff0c\u6703\u57f7\u884c\u6307\u5b9a\u7684\u52d5\u4f5c\u3002<\/p>\n<p>3. **self.present**: \u9019\u500b\u51fd\u6578\u7528\u4f86\u986f\u793a\u6211\u5011\u525b\u525b\u5275\u5efa\u7684\u63d0\u793a\u6846\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u5be6\u4f5c\u904e\u7a0b\u4e2d\uff0c\u82e5\u9047\u5230\u63d0\u793a\u6846\u7121\u6cd5\u986f\u793a\u7684\u60c5\u6cc1\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<br \/>\n&#8211; \u78ba\u4fdd `self` \u662f\u4e00\u500b UIViewController \u7684\u5be6\u4f8b\u3002<br \/>\n&#8211; \u78ba\u8a8d\u4f60\u7684\u63d0\u793a\u6846\u5728\u4e3b\u57f7\u884c\u7dd2\u4e2d\u88ab\u8abf\u7528\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u4f60\u53ef\u4ee5\u6839\u64da\u9700\u6c42\uff0c\u589e\u52a0\u66f4\u591a\u7684\u9078\u9805\u6216\u6a23\u5f0f\u5230\u63d0\u793a\u6846\u4e2d\uff0c\u4f8b\u5982\u65b0\u589e\u53d6\u6d88\u6309\u9215\u6216\u662f\u6587\u672c\u8f38\u5165\u6846\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u65b0\u589e\u4e00\u500b\u6587\u672c\u8f38\u5165\u6846\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\n\/\/ \u65b0\u589e\u6587\u672c\u8f38\u5165\u6846<br \/>\nalertController.addTextField { (textField) in<br \/>\n    textField.placeholder = &#8220;\u8f38\u5165\u50f9\u683c\u6216\u6578\u91cf&#8221;<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\uff0c\u4f7f\u7528\u8005\u5c31\u53ef\u4ee5\u5728\u63d0\u793a\u6846\u4e2d\u76f4\u63a5\u8f38\u5165\u4fe1\u606f\uff0c\u8b93\u4f60\u7684\u61c9\u7528\u66f4\u52a0\u4e92\u52d5\u3002<\/p>\n<p>&#8212;<\/p>\n<p>![\u63d0\u793a\u6846\u7bc4\u4f8b](https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png)<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: \u5982\u4f55\u5728\u63d0\u793a\u6846\u4e2d\u6dfb\u52a0\u591a\u500b\u6309\u9215\uff1f**<br \/>\nA1: \u53ef\u4ee5\u4f7f\u7528 `UIAlertAction` \u6dfb\u52a0\u591a\u500b\u6309\u9215\uff0c\u6bcf\u500b\u6309\u9215\u90fd\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u6a19\u984c\u548c\u8655\u7406\u51fd\u6578\u3002<\/p>\n<p>**Q2: \u63d0\u793a\u6846\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u5916\u89c0\u55ce\uff1f**<br \/>\nA2: \u96d6\u7136 `UIAlertController` \u7684\u5916\u89c0\u6709\u5176\u9650\u5236\uff0c\u4f46\u53ef\u4ee5\u900f\u904e\u81ea\u5b9a\u7fa9\u8996\u5716\u6216\u4f7f\u7528\u7b2c\u4e09\u65b9\u5eab\u4f86\u5be6\u73fe\u66f4\u8c50\u5bcc\u7684 UI\u3002<\/p>\n<p>**Q3: \u63d0\u793a\u6846\u53ef\u4ee5\u7528\u65bc\u932f\u8aa4\u63d0\u793a\u55ce\uff1f**<br \/>\nA3: \u662f\u7684\uff0c`UIAlertController` \u662f\u7528\u4f86\u986f\u793a\u932f\u8aa4\u4fe1\u606f\u7684\u7406\u60f3\u5de5\u5177\uff0c\u5e6b\u52a9\u7528\u6236\u7406\u89e3\u554f\u984c\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528\u63d0\u793a\u6846\uff01\u5982\u6709\u66f4\u591a\u554f\u984c\uff0c\u6b61\u8fce\u5728\u4e0b\u65b9\u7559\u8a00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>let alertController = UIAlertController(<br \/>\n    title: &#8220;\u63d0\u793a&#8221;,<br \/>\n    message: &#8220;\u8acb\u8f38\u5165\u50f9\u683c\u6216\u6578\u91cf&#8221;,<br \/>\n    preferredStyle: .alert)<\/p>\n<p>\/\/ \u5efa\u7acb[\u78ba\u8a8d]\u6309\u9215<br \/>\nlet okAction = UIAlertAction(<br \/>\n    title: &#8220;\u78ba\u8a8d&#8221;,<br \/>\n    style: .default,<br \/>\n    handler: {<br \/>\n        (action: UIAlertAction!) -> Void in<br \/>\n        print(&#8220;\u6309\u4e0b\u78ba\u8a8d\u5f8c\u7684\u52d5\u4f5c&#8221;)<br \/>\n})<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-367","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-5V","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/367","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=367"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/367\/revisions"}],"predecessor-version":[{"id":589,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/367\/revisions\/589"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}