aej*_*yun 5 xcode uibutton iboutlet ios swift
这是我的目标:当Start点击按钮时,我希望Send启用按钮。以下是我的视图控制器加载后的样子:
如您所见,Send按钮被禁用,这是所期望的。我通过在我的中编写以下代码来禁用它TaskListTableViewCell.swift(为了简洁起见,我删除了一些其他不相关的代码):
class TaskListTableViewCell: UITableViewCell {
@IBOutlet weak var sendButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
sendButton.enabled = false
}
}
Run Code Online (Sandbox Code Playgroud)
为了以防万一它可以帮助您看得更清楚,这就是视图控制器在以下位置的样子Main.storyboard:
在我的中TaskListViewController,我有以下代码:
@IBAction func startButtonTapped(sender: AnyObject) {
//write the code here to change sendButton.enabled = true
}
Run Code Online (Sandbox Code Playgroud)
问题是,我似乎找不到改变sendButton.enabled = true. 我试过了:
@IBAction func startButtonTapped(sender: AnyObject) {
let taskListViewController = TaskListViewController()
taskListTableViewCell.sendButton.enabled = true
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
class TaskListTableViewCell: UITableViewCell {
@IBOutlet weak var sendButton: UIButton!
func changeSendButtonEnabled() {
sendButton.enabled = true
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
sendButton.enabled = false
}
}
Run Code Online (Sandbox Code Playgroud)
在我的 中TaskListViewController.swift,我写道:
@IBAction func startButtonTapped(sender: AnyObject) {
let taskListViewController = TaskListViewController()
taskListTableViewCell.changeSendButtonEnabled()
}
Run Code Online (Sandbox Code Playgroud)
但这两种解决方案都会出现以下错误:fatal error: unexpectedly 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 , and using IBOutlet from another class in快点。但是,这些帖子都无法解决我的问题。
请尝试一下这个,它可能会解决您的问题。
//自定义单元格.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
}
}
Run Code Online (Sandbox Code Playgroud)
//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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1579 次 |
| 最近记录: |