如果相同,如何将数据传递给之前的视图控制器class Swift
How to pass data to the previous view controller if it is the same class Swift
我有一个 table 视图控制器,当我单击 navigationBarItem 中的按钮时,我会打开相同的视图控制器,但设置略有不同,具体取决于标志。通过关闭这个视图控制器,我想将值传递给前一个视图控制器并用新数据重新加载它的数据,我该怎么做?委派在这里无济于事,因为它是相同的 class.
class LibraryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
...
var isContentFromPlaylist: Bool = false
...
//I open the same VC
@IBAction func addTracksButtonPressed(_ sender: Any) {
let storyboard = UIStoryboard(name: "LibraryViewController", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "Library") as! LibraryViewController
...
navigationController?.showDetailViewController(vc, sender: nil)
}
...
@objc private func doneButtonPressed() {
...
//Here I want to change value isContentFromPlaylist to true in previous VC when this VC will close
dismiss(animated: true, completion: nil)
}
}
您可以使用这种方法。在 class
中添加一个 属性
class LibraryViewController {
//...
var creator: LibraryViewController?
//...
}
然后,在控制器实例化后设置属性...
@IBAction func addTracksButtonPressed(_ sender: Any) {
let storyboard = UIStoryboard(name: "LibraryViewController", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "Library") as! LibraryViewController
//...
vc.creator = self // Here it is
//...
navigationController?.showDetailViewController(vc, sender: nil)
}
并按照您喜欢的方式使用它:
@objc private func doneButtonPressed() {
...
//Here I want to change value isContentFromPlaylist to true in previous VC when this VC will close
creator?.isContentFromPlaylist = .........
dismiss(animated: true, completion: nil)
}
}
P.S。要重新加载数据,请使用 UI 线程:
DispatchQueue.main.async{
creator?.tableView.reloadData()
}
我有一个 table 视图控制器,当我单击 navigationBarItem 中的按钮时,我会打开相同的视图控制器,但设置略有不同,具体取决于标志。通过关闭这个视图控制器,我想将值传递给前一个视图控制器并用新数据重新加载它的数据,我该怎么做?委派在这里无济于事,因为它是相同的 class.
class LibraryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
...
var isContentFromPlaylist: Bool = false
...
//I open the same VC
@IBAction func addTracksButtonPressed(_ sender: Any) {
let storyboard = UIStoryboard(name: "LibraryViewController", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "Library") as! LibraryViewController
...
navigationController?.showDetailViewController(vc, sender: nil)
}
...
@objc private func doneButtonPressed() {
...
//Here I want to change value isContentFromPlaylist to true in previous VC when this VC will close
dismiss(animated: true, completion: nil)
}
}
您可以使用这种方法。在 class
中添加一个 属性class LibraryViewController {
//...
var creator: LibraryViewController?
//...
}
然后,在控制器实例化后设置属性...
@IBAction func addTracksButtonPressed(_ sender: Any) {
let storyboard = UIStoryboard(name: "LibraryViewController", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "Library") as! LibraryViewController
//...
vc.creator = self // Here it is
//...
navigationController?.showDetailViewController(vc, sender: nil)
}
并按照您喜欢的方式使用它:
@objc private func doneButtonPressed() {
...
//Here I want to change value isContentFromPlaylist to true in previous VC when this VC will close
creator?.isContentFromPlaylist = .........
dismiss(animated: true, completion: nil)
}
}
P.S。要重新加载数据,请使用 UI 线程:
DispatchQueue.main.async{
creator?.tableView.reloadData()
}