加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 从另一个类改变按钮的状态

发布时间:2020-12-14 17:47:12 所属栏目:百科 来源:网络整理
导读:这是我的目标:点击“开始”按钮时,我希望启用“发送”按钮.以下是我的视图控制器在加载时的样子: 如您所见,发送按钮被禁用,这是所需的.我已经通过在我的TaskListTableViewCell.swift中编写以下代码来禁用它(为了简洁起见,我删除了一些其他不相关的代码):
这是我的目标:点击“开始”按钮时,我希望启用“发送”按钮.以下是我的视图控制器在加载时的样子:

enter image description here

如您所见,发送按钮被禁用,这是所需的.我已经通过在我的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()
}

但是这两个解决方案都给出了这个错误:致命错误:在展开Optional值时意外地发现nil.

我已经阅读了一些关于从另一个类访问IBOutlet的其他帖子,例如这篇文章:How to access an IBOutlet from another class,Accessing IBOutlet from another class,Access an IBOutlet from another class in Swift和using IBOutlet from another class in swift.但是,这些帖子都没有能够解决我的问题.

解决方法

请试试这个,它可以解决你的问题.

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

}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读