Uitableview 将数据传递到swift 3中的上一个tableviewcell
我为标签创建了3个出口,为按钮创建了2个操作,为UIDatePickerView创建了1个出口。lblField显示当前日期,包括月份、日期和年份。lblField2在按dueDate操作后显示用户选择的日期。lblField3显示当前日期和所选日期之间的差异 当我点击到期日的单元格时:它会打开DatePickerViewController,如上所述Uitableview 将数据传递到swift 3中的上一个tableviewcell,uitableview,swift3,Uitableview,Swift3,我为标签创建了3个出口,为按钮创建了2个操作,为UIDatePickerView创建了1个出口。lblField显示当前日期,包括月份、日期和年份。lblField2在按dueDate操作后显示用户选择的日期。lblField3显示当前日期和所选日期之间的差异 当我点击到期日的单元格时:它会打开DatePickerViewController,如上所述 现在,我需要在按下“完成”按钮后,将DatePickerViewController的lblField3数据传递到Swift 3中的TodayV
现在,我需要在按下“完成”按钮后,将DatePickerViewController的lblField3数据传递到Swift 3中的TodayViewController的“到期日期:”单元。如何在Swift 3中使用simple way进行操作?为此,您只需使用
委托/协议
。创建一个委托并在DatePickerViewController
中创建其实例,现在使用TodayViewController
实现该协议,当您移动到DatePickerViewController
时,将其委托设置为self
。现在只需调用DatePickerViewController
上的委托方法即可传递数据
protocol DatePickerDelegate {
func getDifference(diff: String)
}
现在在DatePickerViewController
中创建它的实例,并调用获取日期差的方法
class DatePickerViewController: UIViewController {
var delegate: DatePickerDelegate?
//Other instances property declaration ...
//other methods
@IBAction func Done(_ sender: Any) {
self.delegate?.getDifference(diff: lblField3.text ?? "")
_ = self.navigationController?.popViewController(animated: true)
}
}
现在,只需使用TodayViewController
实现DatePickerDelegate
,并添加其方法
class TodayViewController: UIViewController, DatePickerDelegate, UITableViewDelegate, UITableViewDataSource {
//Other methods
//Set the delegate when you are push to DatePickerViewController
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "DatePickerViewController") as! DatePickerViewController
//Set delegate of DatePickerViewController
secondViewController.delegate = self
selectedIndexPath = indexPath
self.navigationController?.pushViewController(secondViewController, animated: true)
//DatePickerDelegate method
func getDifference(diff: String) {
//Access diff here
print(diff)
}
}
编辑:问题在于didSelectRowAt
您正在比较单元格标签的文本,而不是需要比较tableView的行
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (tableView.tag == 1) {
if indexPath.row == 4 {
//Show view for Baby size guide:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "BabySizeViewController") as! BabySizeViewController
selectedIndexPath = indexPath
self.navigationController?.pushViewController(secondViewController, animated: true)
}
else if indexPath.row == 1 {
//Show view for Due date:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "DatePickerViewController") as! DatePickerViewController
selectedIndexPath = indexPath
secondViewController.delegate = self
self.navigationController?.pushViewController(secondViewController, animated: true)
}
}
}
我试图声明全局变量。先生,请帮帮我。我已经给出了上面的所有代码??你可以写代码。。。我会很高兴的。@Tamannah检查我编辑的答案,这样做它会以你想要的方式工作。先生,它没有工作。。。它不显示单元格中的差异。@尼拉夫D@Tamannah您的代码看起来正常,在DatePickerViewController
中设置dueDate后是否按了“完成”按钮?是。。。我做了,但没有显示任何内容。@Nirav D