Change the state of a button from another class

This is my goal: when I click a button, StartI want the buttons to Sendbe on. Below is what my view controller looks like when loading:

enter image description here

As you can see, the buttons are Senddisabled, which is necessary. And I turned it off by writing the following code in mine TaskListTableViewCell.swift(I deleted some other irrelevant code for the sake of brevity):

class TaskListTableViewCell: UITableViewCell {

    @IBOutlet weak var sendButton: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()

        sendButton.enabled = false
    }
}

And just in case, this can help you better understand, here's what a view controller looks like in Main.storyboard:

enter image description here

And in mine TaskListViewControllerI have the following code:

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

The problem is that I cannot find a way to change sendButton.enabled = true. I tried:

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

I 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.swiftI wrote:

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

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

IBOutlet , : IBOutlet , IBOutlet , IBOutlet Swift IBOutlet . .

+4
2

, , .

//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;

}

}
+4

, tableview, , . , . ( ), Bool, , . bool, , tableView.reloadData(). , cellForRowAtIndexPath .

, , , , tableviewcell, , . , . , ,

+1

Source: https://habr.com/ru/post/1621231/


All Articles