2017-08-12 64 views
1

我有7個按鈕可以在一週內打開和關閉幾天。如果,例如,星期日的按鈕被按下,一個IBAction爲自動切換的狀態沒有任何問題:從Swift中的函數設置UiButton的狀態

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

但是,當我試圖從一個函數在同一個類中設置該按鈕的狀態,我得到一個錯誤說:

unexpectedly found nil while unwrapping an Optional value

下面是用於「isSelected」設置爲代碼true或false:

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

我不明白爲什麼這是行不通的,因爲它是預很多相同的代碼。

編輯: 該函數由TableViewController調用,並且按鈕位於自定義的TableViewCell中。我包括了整個代碼,只是爲了說清楚。 TableViewController:

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) 
} 

TableViewCell:

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 
} 

}

+0

你檢查你所有的IBOutlets是否連接正確 –

+0

是的,那是我檢查的第一件事。每個按鈕連接正確。 –

+0

你可以在setSo中放置一個斷點,然後檢查是否所有按鈕都已啓動並且不是零? –

回答

0

首先,你必須註冊表格視圖筆尖能夠使用它。

添加在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方法。

+0

我收到錯誤:「參數標籤不匹配任何可用的重載。」 –

+0

@ J.Doe你的'TableViewCellDays'是一個視圖控制器嗎? – njuri

+0

TableViewCellDays是一個自定義的TableViewCell筆尖。該函數由ViewController調用。 –

相關問題