从另一个类更改按钮的状态

时间:2015-12-22 08:27:24

标签: ios xcode swift uibutton iboutlet

这是我的目标:点击Start按钮后,我希望启用Send按钮。以下是我的视图控制器在加载时的样子:

enter image description here

如您所见,Send按钮被禁用,这是必需的。我通过在TaskListTableViewCell.swift中编写以下代码来禁用它(为了简洁起见,我删除了一些其他不相关的代码):

class TaskListTableViewCell: UITableViewCell {

    @IBOutlet weak var sendButton: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()

        sendButton.enabled = false
    }
}

为了防止你看到更好,这就是Main.storyboard视图控制器的样子:

enter image description here

在我的TaskListViewController中,我有以下代码:

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

问题是,我似乎找不到改变sendButton.enabled = true的方法。我试过了:

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

我也尝试过:

class TaskListTableViewCell: UITableViewCell {

    @IBOutlet weak var sendButton: UIButton!

    func changeSendButtonEnabled() {
        sendButton.enabled = true
    }

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

在我的TaskListViewController.swift中,我写道:

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

但这两种解决方案都会出现此错误:fatal error: unexpectedly found nil while unwrapping an Optional value

我已经阅读了其他一些关于从另一个类访问IBOutlet的帖子,例如此帖:How to access an IBOutlet from another classAccessing IBOutlet from another classAccess an IBOutlet from another class in Swiftusing IBOutlet from another class in swift。但是,这些帖子都没有能够解决我的问题。

2 个答案:

答案 0 :(得分:4)

请尝试这个,它可以解决您的问题。

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

}

}

答案 1 :(得分:1)

包含tableview单元格的tableview需要一个数据源才能用数据填充它。这种情况下的数据是按钮是否启用。您可以创建一个包含Bool的对象(或结构)数组,以告知该按钮是否已启用。启用开始按钮后更改布尔值,然后调用tableView.reloadData()。然后,您的数据源将重新加载对象数组,然后cellForRowAtIndexPath将被称为重新配置您的单元格。

您获得致命错误的原因可能是因为您尝试访问的tableviewcell的引用为零。当您调用方法更改按钮状态时,它在那里找不到任何内容并崩溃。虽然很难确切知道发生了什么,因为缺少代码

相关问题