尝试从单独的 viewcontroller 更改 tableview 单元格标签时自定义委托方法调用两次
Custom delegate method called twice when trying to change tableview cell label from separate viewcontroller
我创建了一个自定义委托方法来更改表格视图单元格的标签文本,但该方法似乎被调用了两次。添加断点后,它似乎正在正确更改包含新标签文本的变量,但随后第二次调用它,这次变量为 nil。第二次调用改变了 label.text 但它为零。这是它的设置方式:
protocol ChangeInfoViewControllerDelegate {
func changeInfoValue(vital: String)
}
//this class is where the new text value being created
class ManualVitalsInputViewController: UIViewController {
let vitalVC = VitalsViewController()
var delegate: ChangeInfoViewControllerDelegate!
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = vitalVC
}
@IBAction func saveButtonTapped(_ sender: Any) {
delegate.changeVitalValue(vital: "Test") //This gets called twice, 2nd time is nil
dismiss(animated: true)
}
}
//this is the controller where the text is being changed
class VitalsViewController: UIViewController {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let vital = vitals[indexPath.row]
cell.imageView.image = vital.image
if changedManualVital != nil {
cell.vitalTitleLabel.text = changedManualVital//where text should change to new text from delegate
} else {
cell.vitalTitleLabel.text = vital.title
}
return cell
}
}
//where delegate used
extension VitalsViewController: ChangeInfoViewControllerDelegate {
func changeVitalValue(vital: String) {
self.changedManualVital = vital
}
}
关于委托被调用两次以及如何只调用一次的任何想法。或者,如果您有更好的建议来更改另一个表格视图单元格标签viewcontroller,我愿意接受建议。
你需要摆脱两者
let vitalVC = VitalsViewController()
和
self.delegate = vitalVC
然后当你实例化手册时vc
let vc = self.storyboard............... as! ManualVitalsInputViewController
vc.delegate = self /// here is the actual link
// present/push
我创建了一个自定义委托方法来更改表格视图单元格的标签文本,但该方法似乎被调用了两次。添加断点后,它似乎正在正确更改包含新标签文本的变量,但随后第二次调用它,这次变量为 nil。第二次调用改变了 label.text 但它为零。这是它的设置方式:
protocol ChangeInfoViewControllerDelegate {
func changeInfoValue(vital: String)
}
//this class is where the new text value being created
class ManualVitalsInputViewController: UIViewController {
let vitalVC = VitalsViewController()
var delegate: ChangeInfoViewControllerDelegate!
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = vitalVC
}
@IBAction func saveButtonTapped(_ sender: Any) {
delegate.changeVitalValue(vital: "Test") //This gets called twice, 2nd time is nil
dismiss(animated: true)
}
}
//this is the controller where the text is being changed
class VitalsViewController: UIViewController {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let vital = vitals[indexPath.row]
cell.imageView.image = vital.image
if changedManualVital != nil {
cell.vitalTitleLabel.text = changedManualVital//where text should change to new text from delegate
} else {
cell.vitalTitleLabel.text = vital.title
}
return cell
}
}
//where delegate used
extension VitalsViewController: ChangeInfoViewControllerDelegate {
func changeVitalValue(vital: String) {
self.changedManualVital = vital
}
}
关于委托被调用两次以及如何只调用一次的任何想法。或者,如果您有更好的建议来更改另一个表格视图单元格标签viewcontroller,我愿意接受建议。
你需要摆脱两者
let vitalVC = VitalsViewController()
和
self.delegate = vitalVC
然后当你实例化手册时vc
let vc = self.storyboard............... as! ManualVitalsInputViewController
vc.delegate = self /// here is the actual link
// present/push