xcode 在 Swift 中关闭模态呈现的视图控制器后,如何重新加载 ViewController?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28706877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you reload a ViewController after dismissing a modally presented view controller in Swift?
提问by A_toaster
I have a first tableViewController which opens up a second tableViewcontroller upon clicking a cell. The second view controller is presented modally (Show Detail segue) and is dismissed with:
我有第一个 tableViewController,它在单击单元格时打开第二个 tableViewController。第二个视图控制器以模态呈现(Show Detail segue)并被关闭:
self.dismissViewControllerAnimated(true, completion: {})
At this point, the second view controller slides away and reveals the first view controller underneath it. I would then like to reload the first view controller. I understand that this may require use of delegate functions, but not sure exactly how to implement it
此时,第二个视图控制器滑开并显示其下方的第一个视图控制器。然后我想重新加载第一个视图控制器。我知道这可能需要使用委托函数,但不确定如何实现它
采纳答案by Mikael Stenberg
I solved it a bit differently since I don't want that dependancy.
我以不同的方式解决了它,因为我不想要这种依赖性。
And this approach is intended when you present a controller modally, since the presenting controller wont reload when you dismiss the presented.
这种方法适用于以模态方式呈现控制器时,因为当您关闭呈现的控制器时,呈现的控制器不会重新加载。
Anyway solution!
无论如何解决!
Instead you make a Singleton (mediator)
相反,您制作了一个单身人士(调解员)
protocol ModalTransitionListener {
func popoverDismissed()
}
class ModalTransitionMediator {
/* Singleton */
class var instance: ModalTransitionMediator {
struct Static {
static let instance: ModalTransitionMediator = ModalTransitionMediator()
}
return Static.instance
}
private var listener: ModalTransitionListener?
private init() {
}
func setListener(listener: ModalTransitionListener) {
self.listener = listener
}
func sendPopoverDismissed(modelChanged: Bool) {
listener?.popoverDismissed()
}
}
Have you Presenting controller implement the protocol like this:
你有没有呈现控制器实现这样的协议:
class PresentingController: ModalTransitionListener {
//other code
func viewDidLoad() {
ModalTransitionMediator.instance.setListener(self)
}
//required delegate func
func popoverDismissed() {
self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
yourTableViev.reloadData() (if you use tableview)
}
}
and finally in your PresentedViewController in your viewDid/WillDisappear func or custom func add:
最后在您的 ViewDid/WillDisappear func 或自定义 func 中的 PresentedViewController 添加:
ModalTransitionMediator.instance.sendPopoverDismissed(true)
回答by Cihan Tek
You can simply reaload your data in viewDidAppear:
, but that might cause the table to be refreshed unnecessarily in some cases.
您可以简单地在 中重新加载数据viewDidAppear:
,但这可能会导致在某些情况下不必要地刷新表。
A more flexible solution is to use protocols as you have correctly guessed.
更灵活的解决方案是使用您正确猜测的协议。
Let's say the class name of your first tableViewController is Table1VC
and the second one is Table2VC
. You should define a protocol called Table2Delegate
that will contain a single method such as table2WillDismissed
.
假设您的第一个 tableViewController 的类名是Table1VC
,第二个是Table2VC
. 您应该定义一个名为的协议Table2Delegate
,该协议将包含一个方法,例如table2WillDismissed
.
protocol Table2Delegate {
func table2WillDismissed()
}
Then you should make your Table1VC
instance conform to this protocol and reload your table within your implementation of the delegate method.
然后你应该让你的Table1VC
实例符合这个协议并在你的委托方法的实现中重新加载你的表。
Of course in order for this to work, you should add a property to Table2VC
that will hold the delegate:
当然,为了让它工作,你应该添加一个属性来Table2VC
保存委托:
weak var del: Table2Delegate?
and set its value to your Table1VC
instance.
并将其值设置为您的Table1VC
实例。
After you have set your delegate, just add a call to the delegate method right before calling the dismissViewControllerAnimated
in your Table2VC
instance.
你已经确立了自己的委托后,正好在调用前添加到委托方法的调用dismissViewControllerAnimated
您的Table2VC
实例。
del?.table2WillDismissed()
self.dismissViewControllerAnimated(true, completion: {})
This will give you precise control over when the table will get reloaded.
这将使您能够精确控制何时重新加载表。
回答by caesic
Swift 5:
斯威夫特 5:
You can access the presenting ViewController (presentingViewController) property and use it to reload the table view when the view will disappear.
您可以访问呈现视图控制器 (presentingViewController) 属性,并在视图消失时使用它重新加载表视图。
class: FirstViewController {
var tableView: UITableView
present(SecondViewController(), animated: true, completion: nil)
}
In your second view controller, you can in the viewWillDisappear method, add the following code:
在第二个视图控制器中,您可以在 viewWillDisappear 方法中添加以下代码:
class SecondViewController {
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let firstVC = presentingViewController as? FirstViewController {
DispatchQueue.main.async {
firstVC.tableView.reloadData()
}
}
}
}
When you dismiss the SecondViewController, the tableview of the FirstViewController will reload.
当您关闭 SecondViewController 时,FirstViewController 的 tableview 将重新加载。