{"id":6802,"date":"2025-06-01T13:24:15","date_gmt":"2025-06-05T05:30:14","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6802"},"modified":"2025-06-05T13:24:15","modified_gmt":"2025-06-05T05:30:14","slug":"https-badgameshow-com-steven-77","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-77\/","title":{"rendered":"Swift UISwitch \u63a7\u5236\u5668\u4f7f\u7528\u6307\u5357 &#8211; 2025 \u6700\u65b0\u5207\u63db\u958b\u95dc\u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UISwitch, \u63a7\u5236\u5668, \u958b\u95dc, \u6559\u5b78, iOS, \u7a0b\u5f0f\u8a2d\u8a08\"><\/p>\n<h1>Swift UISwitch \u63a7\u5236\u5668\u4f7f\u7528\u6307\u5357 &#8211; 2025 \u6700\u65b0\u5207\u63db\u958b\u95dc\u6559\u5b78<\/h1>\n<p>UISwitch \u63a7\u5236\u5668\u662f iOS \u958b\u767c\u4e2d\u5e38\u898b\u7684\u7d44\u4ef6\uff0c\u8b93\u4f7f\u7528\u8005\u80fd\u5920\u5728\u5169\u500b\u9078\u9805\u4e4b\u9593\u8f15\u9b06\u5207\u63db\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5275\u5efa UISwitch \u63a7\u5236\u5668\uff0c\u4e26\u6dfb\u52a0\u4e8b\u4ef6\u4f86\u63a7\u5236\u958b\u95dc\u7684\u72c0\u614b\uff0c\u8b93\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u66f4\u52a0\u4e92\u52d5\u3002<\/p>\n<h2>\u5982\u4f55\u5275\u5efa UISwitch \u63a7\u5236\u5668<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b UISwitch \u63a7\u5236\u5668\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 Swift \u4e2d\u5275\u5efa\u548c\u6dfb\u52a0 UISwitch \u7684\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let switchControl = UISwitch()\nview.addSubview(switchControl)\nswitchControl.frame = CGRect(x: 100, y: 100, width: 0, height: 0)\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u6703\u5728\u4f60\u7684\u8996\u5716\u4e2d\u6dfb\u52a0\u4e00\u500b UISwitch \u63a7\u5236\u5668\uff0c\u4e26\u8a2d\u7f6e\u5176\u521d\u59cb\u4f4d\u7f6e\u3002\u6ce8\u610f\uff0c\u9019\u88e1\u7684 frame \u8a2d\u5b9a\u70ba (100, 100)\uff0c\u4f60\u53ef\u4ee5\u6839\u64da\u81ea\u5df1\u7684\u9700\u6c42\u6539\u8b8a\u4f4d\u7f6e\u3002<\/p>\n<h2>\u6fc0\u6d3b UISwitch \u63a7\u5236\u5668<\/h2>\n<p>\u70ba\u4e86\u8b93 UISwitch \u63a7\u5236\u5668\u986f\u793a\u70ba\u958b\u555f\u6216\u95dc\u9589\u72c0\u614b\uff0c\u4f60\u53ef\u4ee5\u8a2d\u7f6e\u5b83\u7684 <strong>isOn<\/strong> \u5c6c\u6027\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">switchControl.isOn = true\n<\/code><\/pre>\n<p>\u9019\u6a23\uff0cUISwitch \u63a7\u5236\u5668\u5c07\u6703\u986f\u793a\u70ba\u958b\u555f\u72c0\u614b\u3002<\/p>\n<h2>\u6dfb\u52a0\u4e8b\u4ef6\u76e3\u807d<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u6dfb\u52a0\u4e8b\u4ef6\u4f86\u76e3\u807d UISwitch \u63a7\u5236\u5668\u7684\u72c0\u614b\u8b8a\u5316\u3002\u9019\u6a23\uff0c\u7576\u4f7f\u7528\u8005\u5207\u63db\u958b\u95dc\u6642\uff0c\u4f60\u53ef\u4ee5\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u793a\u7bc4\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">switchControl.addTarget(self, action: #selector(switchChanged(_:)), for: .valueChanged)\n\n@objc func switchChanged(_ sender: UISwitch) {\n    if sender.isOn {\n        print(\"Switch is on\")\n    } else {\n        print(\"Switch is off\")\n    }\n}\n<\/code><\/pre>\n<p>\u7576\u4f7f\u7528\u8005\u5207\u63db\u958b\u95dc\u6642\uff0c\u63a7\u5236\u53f0\u5c07\u6839\u64da\u7576\u524d\u72c0\u614b\u6253\u5370\u51fa\u300cSwitch is on\u300d\u6216\u300cSwitch is off\u300d\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 UISwitch \u63a7\u5236\u5668\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\u4e8b\u4ef6\u672a\u88ab\u89f8\u767c\u3002\u9019\u901a\u5e38\u662f\u56e0\u70ba\u6c92\u6709\u6b63\u78ba\u8a2d\u7f6e target-action\u3002\u8acb\u78ba\u4fdd\u5728 addTarget \u65b9\u6cd5\u4e2d\u50b3\u905e\u7684 selector \u540d\u7a31\u6b63\u78ba\u7121\u8aa4\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>UISwitch \u63a7\u5236\u5668\u53ef\u4ee5\u7528\u65bc\u591a\u7a2e\u61c9\u7528\u5834\u666f\uff0c\u4f8b\u5982\u8a2d\u5b9a\u9801\u9762\u3001\u958b\u95dc\u529f\u80fd\u7b49\u3002\u4f60\u53ef\u4ee5\u5c07\u5176\u8207\u5176\u4ed6 UI \u5143\u7d20\u7d50\u5408\u4f7f\u7528\uff0c\u50cf\u662f UILabel\uff0c\u4f86\u5c55\u793a\u7576\u524d\u958b\u95dc\u72c0\u614b\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let label = UILabel()\nlabel.frame = CGRect(x: 100, y: 150, width: 200, height: 20)\nview.addSubview(label)\n\n@objc func switchChanged(_ sender: UISwitch) {\n    label.text = sender.isOn ? \"Switch is on\" : \"Switch is off\"\n}\n<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u7528\u6236\u4e0d\u50c5\u80fd\u5920\u770b\u5230\u958b\u95dc\u7684\u72c0\u614b\uff0c\u9084\u80fd\u7acb\u5373\u7372\u5f97\u53cd\u994b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UISwitch\u63a7\u5236\u5668\u4f7f\u7528\u2705-\u5207\u63db\u958b\u95dc\u6559\u5b78_1675396034.906545.webp\" alt=\"Swift UISwitch\u63a7\u5236\u5668\u4f7f\u7528\u2705 - \u5207\u63db\u958b\u95dc\u6559\u5b78\" title=\"Swift UISwitch, \u958b\u95dc\u6559\u5b78, iOS\u958b\u95dc, Swift\u958b\u95dc, UISwitch\u63a7\u5236\u5668\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: UISwitch \u7684 isOn \u5c6c\u6027\u6709\u4ec0\u9ebc\u4f5c\u7528\uff1f<\/h3>\n<p>A1: isOn \u5c6c\u6027\u7528\u4f86\u63a7\u5236 UISwitch \u7684\u958b\u555f\u6216\u95dc\u9589\u72c0\u614b\u3002\u7576\u8a2d\u70ba true \u6642\uff0c\u958b\u95dc\u986f\u793a\u70ba\u958b\u555f\u72c0\u614b\uff1b\u53cd\u4e4b\u5247\u70ba\u95dc\u9589\u72c0\u614b\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5728 Swift \u4e2d\u6aa2\u67e5 UISwitch \u72c0\u614b\uff1f<\/h3>\n<p>A2: \u4f60\u53ef\u4ee5\u901a\u904e UISwitch \u7684 isOn \u5c6c\u6027\u4f86\u6aa2\u67e5\u7576\u524d\u7684\u958b\u95dc\u72c0\u614b\u3002\u4f8b\u5982\uff0c\u7576\u958b\u95dc\u72c0\u614b\u6539\u8b8a\u6642\uff0c\u4f7f\u7528\u8005\u89f8\u767c\u7684\u4e8b\u4ef6\u53ef\u4ee5\u76f4\u63a5\u6aa2\u67e5\u9019\u500b\u5c6c\u6027\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u5c07 UISwitch \u63a7\u5236\u5668\u8207\u5176\u4ed6 UI \u5143\u7d20\u7d50\u5408\u4f7f\u7528\uff1f<\/h3>\n<p>A3: \u4f60\u53ef\u4ee5\u5c07 UISwitch \u8207 UILabel \u6216\u5176\u4ed6 UI \u5143\u7d20\u7d44\u5408\uff0c\u6839\u64da UISwitch \u7684\u72c0\u614b\u66f4\u65b0\u5176\u4ed6\u5143\u4ef6\u7684\u986f\u793a\uff0c\u5f9e\u800c\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift \u5275\u5efa\u548c\u4f7f\u7528 UISwitch \u63a7\u5236\u5668\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5be6\u7528\u7bc4\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u5728 iOS \u958b\u767c\u4e2d\u66f4\u597d\u5730\u4f7f\u7528 UISwitch \u63a7\u5236\u5668\uff01<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2dUISwitch\u63a7\u5236\u5668\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5728iOS\u61c9\u7528\u7a0b\u5e8f\u4e2d\u6dfb\u52a0\u958b\u95dc\u529f\u80fd\uff0c\u4e26\u4e86\u89e3\u5982\u4f55\u8a2d\u7f6e\u958b\u95dc\u7684\u52d5\u4f5c\u548c\u5c6c\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":6801,"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-6802","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-UISwitch\u63a7\u5236\u5668\u4f7f\u7528\u2705-\u5207\u63db\u958b\u95dc\u6559\u5b78_1675396034.906545.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1LI","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6802","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=6802"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6802\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6801"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}