这是我的目标:当点击 Start
按钮时,我希望启用 Send
按钮。以下是我的 View Controller 在加载时的样子:
如您所见,Send
按钮被禁用,这是我们希望的。我通过在我的 TaskListTableViewCell.swift
中编写以下代码来禁用它(为了简洁起见,我删除了一些其他不相关的代码):
class TaskListTableViewCell: UITableViewCell {
@IBOutlet weak var sendButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
sendButton.enabled = false
}
}
为了以防万一它可能会帮助您看得更清楚,这就是 View Controller 在 Main.storyboard
中的样子:
在我的 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: expectedly found nil while unwrapping an Optional value
。
我读过一些关于从另一个类访问 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 .但是,这些帖子都无法解决我的问题。
最佳答案
请试试这个,它可能会解决您的问题。
//自定义单元格.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;
}
}
关于ios - 从另一个类更改按钮的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34411423/