1. 程式人生 > >[Swift通天遁地]二、表格表單-(14)實時調整表單元素的啟用和失效

[Swift通天遁地]二、表格表單-(14)實時調整表單元素的啟用和失效

本文將演示如何啟用或使表單的區域失效。

在專案導航區,開啟檢視控制器的程式碼檔案【ViewController.swift】

現在開始編寫程式碼,實現啟用或使表單的區域失效。

 1 import UIKit
 2 //首先在當前類檔案中,
 3 //引入以及安裝的第三方類庫
 4 import Eureka
 5 
 6 //修改當前檢視控制器類的父類的名稱
 7 class ViewController: FormViewController {
 8     
 9     override func viewDidLoad() {
10         super.viewDidLoad()
11 12 //往表單中新增一個段落 13 form = Section() 14 //在段落中新增一個分段行 15 <<< SegmentedRow<String>("segments") 16 { 17 //設定該行的選項 18 $0.options = ["Enabled", "Disabled"] 19 //設定該行的預設值 20 $0
.value = "Disabled" 21 } 22 //在段落中新增一個文字行 23 <<< TextRow() 24 { 25 //設定該行的標題文字 26 $0.title = "choose enabled, disable above..." 27 //並設定它的失效屬性,當分段的值為失效時,該文字行也將失效。 28 $0.disabled = "
$segments = 'Disabled'" 29 } 30 //在段落中新增一個Switch行 31 <<< SwitchRow("Disable Next Section?") 32 { 33 //設定該行的標題文字 34 $0.title = $0.tag 35 //並設定它的失效屬性,當分段的值為失效時,該Switch行也將失效。 36 $0.disabled = "$segments = 'Disabled'" 37 } 38 //新增一個新的段落 39 +++ Section() 40 //在段落中新增一個文字行 41 <<< TextRow() 42 { 43 //設定該行的標題文字 44 $0.title = "Gonna be disabled soon.." 45 //並設定它的失效狀態, 46 //根據上一個段落中的開關行的狀態,來決定本行的失效狀態。 47 $0.disabled = Eureka.Condition.function(["Disable Next Section?"], { (form) -> Bool in 48 let row: SwitchRow! = form.rowBy(tag: "Disable Next Section?") 49 return row.value ?? false 50 }) 51 } 52 53 //新增一個新的段落 54 +++ Section() 55 //在段落中新增一個分段行 56 <<< SegmentedRow<String>() 57 { 58 //設定該行的選項 59 $0.options = ["Always Disabled"] 60 //並設定它的失效狀態 61 $0.disabled = true 62 } 63 } 64 65 override func didReceiveMemoryWarning() { 66 super.didReceiveMemoryWarning() 67 // Dispose of any resources that can be recreated. 68 } 69 }