从 Swift 中的函数设置 UiButton 的状态



我有 7 个按钮来打开和关闭一周中的几天。例如,如果按下星期日按钮,IBAction 会自动切换状态,没有任何问题:

@IBAction func SoToggle(_ sender: UIButton) {
self.So.isSelected = !self.So.isSelected;
}

但是当我尝试从同一类中的函数设置该按钮的状态时,我收到一条错误消息:

解开可选值时意外发现 nil

以下是用于将"isSelected"设置为true或false的代码:

func setSo(state: Bool) {
self.So.isSelected = state
}

我不明白为什么这不起作用,因为它几乎是相同的代码。

编辑: 该函数由 TableViewController 调用,按钮位于自定义 TableViewCell 中。我包含了整个代码,只是为了把事情说清楚。 表视图控制器:

class TableViewSettings: UITableViewController{
let sections = ["weekdays", "start time"]
var CellDays:TableViewCellDays = TableViewCellDays()
override func viewDidLoad() {
super.viewDidLoad()
self.view.layer.cornerRadius = 7
self.tableView.contentInset = UIEdgeInsets(top: 10,left: 0,bottom: 0,right: 0)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return sections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.section == 0) {
return Bundle.main.loadNibNamed("TableViewCellDays", owner: self, options: nil)?.first as! TableViewCellDays
} else {
return Bundle.main.loadNibNamed("TableViewCellPicker", owner: self, options: nil)?.first as! TableViewCellPicker
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if(indexPath.section == 0) {
return 80
} else {
return 150
}
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 15
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let v: UIView = UIView()
v.backgroundColor = .clear
return v;
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = Bundle.main.loadNibNamed("TableViewCellHeader", owner: self, options: nil)?.first as! TableViewCellHeader
cell.mainLabel.text = sections[section]
return cell
}
func setSettingsButtons(settings: [String]) {
//        if (settings[0] == "1") {
//            CellDays.Mo.isSelected = true
//        } else {CellDays.Mo.isSelected = false}
//        
//        if (settings[1] == "1") {
//            CellDays.Di.isSelected = true
//        } else {CellDays.Di.isSelected = false}
//        
//        if (settings[2] == "1") {
//            CellDays.Mi.isSelected = true
//        } else {CellDays.Mi.isSelected = false}
//        
//        if (settings[3] == "1") {
//            CellDays.Do.isSelected = true
//        } else {CellDays.Do.isSelected = false}
//        
//        if (settings[4] == "1") {
//            CellDays.Fr.isSelected = true
//        } else {CellDays.Fr.isSelected = false}
//        
//        if (settings[5] == "1") {
//            CellDays.Sa.isSelected = true
//        } else {CellDays.Sa.isSelected = false}
//        
//        if (settings[6] == "1") {
//            CellDays.So.isSelected = true
//        } else {CellDays.So.isSelected = false}
CellDays.setSo(state: true)
}

表视图单元格:

class TableViewCellDays: UITableViewCell {
@IBOutlet var Mo: UIButton!
@IBOutlet var Di: UIButton!
@IBOutlet var Mi: UIButton!
@IBOutlet var Do: UIButton!
@IBOutlet var Fr: UIButton!
@IBOutlet var Sa: UIButton!
@IBOutlet var So: UIButton!
@IBOutlet var MoLeading: NSLayoutConstraint!
@IBOutlet var DiLeading: NSLayoutConstraint!
@IBOutlet var MiLeading: NSLayoutConstraint!
@IBOutlet var DoLeading: NSLayoutConstraint!
@IBOutlet var FrLeading: NSLayoutConstraint!
@IBOutlet var SaLeading: NSLayoutConstraint!
@IBOutlet var SoLeading: NSLayoutConstraint!

var offset: CGFloat = 10
override func awakeFromNib() {
super.awakeFromNib()
// init Button states
Mo.isSelected = false
Di.isSelected = false
Mi.isSelected = false
Do.isSelected = false
Fr.isSelected = false
Sa.isSelected = false
So.isSelected = false

let screenWidth = UIScreen.main.bounds.width
let screenWidthCenter = screenWidth/2
let frameWidth = Do.frame.width
// Offset Settings depending on Device
offset = screenWidth / 37
print(offset)

// X Coordinate Settings
MoLeading.constant = screenWidthCenter - 3.5 * frameWidth - 3.75 * offset
DiLeading.constant = screenWidthCenter - 2.5 * frameWidth - 2.75 * offset
MiLeading.constant = screenWidthCenter - 1.5 * frameWidth - 1.75 * offset
DoLeading.constant = screenWidthCenter - 0.5 * frameWidth - 0.75 * offset
FrLeading.constant = screenWidthCenter + 0.5 * frameWidth + 0.25 * offset
SaLeading.constant = screenWidthCenter + 1.5 * frameWidth + 1.25 * offset
SoLeading.constant = screenWidthCenter + 2.5 * frameWidth + 2.25 * offset

}
@IBAction func MoToggle(_ sender: UIButton) {
self.Mo.isSelected = !self.Mo.isSelected;
}
@IBAction func DiToggle(_ sender: UIButton) {
self.Di.isSelected = !self.Di.isSelected;
}
@IBAction func MiToggle(_ sender: UIButton) {
self.Mi.isSelected = !self.Mi.isSelected;
}
@IBAction func DoToggle(_ sender: UIButton) {
self.Do.isSelected = !self.Do.isSelected;
}
@IBAction func FrToggle(_ sender: UIButton) {
self.Fr.isSelected = !self.Fr.isSelected;
}
@IBAction func SaToggle(_ sender: UIButton) {
self.Sa.isSelected = !self.Sa.isSelected;
}
@IBAction func SoToggle(_ sender: UIButton) {
self.So.isSelected = !self.So.isSelected;
}
func setSo(state: Bool) {
self.Mo.isSelected = state
}

}

首先,您必须为表视图注册笔尖才能使用它。

添加viewDidLoad

self.tableView.register(UINib(nibName:"TableViewCellDays", bundle:nil), forCellReuseIdentifier: "TableViewCellDays")

按如下方式替换变量声明:

var CellDays:TableViewCellDays?

稍后将在数据源方法中为其赋值。

接下来tableView: UITableView, cellForRowAt获取此单元格并将实例变量分配给它CellDays

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.section == 0) {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellDays") as! TableViewCellDays
self.CellDays = cell
return cell
} else {
return Bundle.main.loadNibNamed("TableViewCellPicker", owner: self, options: nil)?.first as! TableViewCellPicker
}
}

只有在此之后,您才能调用setSettingsButtons方法。

最新更新