5

This is my goal: When the Start button is tapped, I want the Send buttons to be enabled. The following is what my view controller looks like when loaded:

enter image description here

As you can see, the Send buttons are disabled, which is desired. And I've disabled it by writing the following code in my TaskListTableViewCell.swift (I deleted some other irrelevant code for the sake of being succinct):

class TaskListTableViewCell: UITableViewCell {

    @IBOutlet weak var sendButton: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()

        sendButton.enabled = false
    }
}

And just in case it might help you see better, this is how the view controller looks like in the Main.storyboard:

enter image description here

And in my TaskListViewController, I have the following code:

@IBAction func startButtonTapped(sender: AnyObject) {
   //write the code here to change sendButton.enabled = true
}

The problem is, I cannot seem to find a way to change the sendButton.enabled = true. I've tried:

@IBAction func startButtonTapped(sender: AnyObject) {
    let taskListViewController = TaskListViewController()
    taskListTableViewCell.sendButton.enabled = true
}

I've also tried:

class TaskListTableViewCell: UITableViewCell {

    @IBOutlet weak var sendButton: UIButton!

    func changeSendButtonEnabled() {
        sendButton.enabled = true
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        sendButton.enabled = false
    }
}

And in my TaskListViewController.swift, I wrote:

@IBAction func startButtonTapped(sender: AnyObject) {
    let taskListViewController = TaskListViewController()
    taskListTableViewCell.changeSendButtonEnabled()
}

But both of these solutions give this error: fatal error: unexpectedly found nil while unwrapping an Optional value.

I've read some other posts about accessing an IBOutlet from another class such as this post: How to access an IBOutlet from another class, Accessing IBOutlet from another class, Access an IBOutlet from another class in Swift, and using IBOutlet from another class in swift. However, none of these posts were able to solve my problem.

Community
  • 1
  • 1
aejhyun
  • 612
  • 1
  • 6
  • 19
  • 3
    If u have the data array, easier solution would be adding a `BOOL` value into the object of your data array (default false), then in `cellForRowAtIndexPath` u set the `sendButton.enabled` follow by that, when u press the start button, set all the BOOL in your data to true then call `reloadTable`, if u want to set ALL the button enabled in your cell class all at once then i dont think its possible, since that only handle one cell at a time (the cell object), mean u can change enabled for 1 button, not all, even with delegate and block, please correct me if im wrong – Tj3n Dec 22 '15 at 08:40
  • Thank you for your response. A question though. it seems like your method works whereas mine does not. What's going on that is causing my method to fail? And your method to succeed? – Jae Dec 22 '15 at 11:20

2 Answers2

4

Please try this , It may solve your problem.

//CustomCell.swift

import UIKit
import Foundation

class CustomCell: UITableViewCell {

@IBOutlet weak var btnTap : UIButton!
@IBOutlet weak var lblTitle : UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    btnTap.enabled = false
    }

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
    }
}

//ViewController.swift

import UIKit

class ViewController: UIViewController, UITableViewDataSource,    UITableViewDelegate{

@IBOutlet weak var table: UITableView!
@IBOutlet weak var btnSTART: UIButton!

var isStartBtnTapped : Bool = false

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func startBtnTapped(sender: AnyObject) {
    isStartBtnTapped = true
    self.table.reloadData()


}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1;
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 3;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let reuseIdentifier:String="cell";

    var cell:CustomCell!

    cell=tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! CustomCell;

    if cell==nil
    {
        cell=CustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier);
    }

    if(isStartBtnTapped == true){

        cell.lblTitle.text = "Enabled"
        cell.btnTap.enabled = true

    }else{

        cell.lblTitle.text = "Disabled"
        cell.btnTap.enabled = false
    }

    return cell;

}

}
technerd
  • 14,144
  • 10
  • 61
  • 92
1

Your tableview that contains tableview cells needs a data source in order to fill it with data. The data in this case being if the buttons are enabled or not. You can create an array of objects (or structs) that contain a Bool to tell the button to be enabled or not. Change the bool when start button is enabled and then call tableView.reloadData(). Your data source will then reload the array of objects and cellForRowAtIndexPath will then be called reconfiguring your cells.

The reason that your getting the fatal error is probably because the reference to the tableviewcell that you are trying to access is nil. When you call the method to change the button status, it finds nothing there and crashes. Although it's hard to tell exactly what's happening because there is code missing

bolnad
  • 4,533
  • 3
  • 29
  • 41