简体   繁体   English

当 UIViewController 返回顶部时,iOS 中是否有委托函数来获得通知?

[英]Is there a delegate function in iOS to get notified when a UIViewController is coming back to top?

I'm somewhat a Swift/iOS newbie but this seems simple.我有点像 Swift/iOS 新手,但这似乎很简单。 I have a UIViewController and upon some action I am instantiating another modal UIViewController, which at some point I will dismiss.我有一个 UIViewController 并且在执行某些操作时我正在实例化另一个模态 UIViewController,在某些时候我会关闭它。 I just need to originating VC to be notified when this happens.我只需要在发生这种情况时通知原始 VC。 Realizing that this could be handled using navigation control, I'm looking for a way to do it modally ie意识到这可以使用导航控件来处理,我正在寻找一种方法来进行模态处理,即

I want to push the modal vc on to the hierarchy:我想将模态 vc 推送到层次结构:

let vc = storyboard.instantiateViewController(withIdentifier: "DetailView")
self.present(vc, animated: true, completion: nil)

and when I do this in the modal vc:当我在模态 vc 中执行此操作时:

dismiss(animated: true, completion: nil)

I just want the original vc sitting in the background to be notified it's coming back into focus (or top) and be able to execute code.我只是想让坐在后台的原始 vc 被通知它重新回到焦点(或顶部)并且能够执行代码。

What am I missing?我错过了什么?

UPDATED : the answer here is very simple.更新:这里的答案很简单。 A ViewController that is presented modally with a presentation style of "Over Current Context" does not fire ViewWillAppear in the presenting VC when it is dismissed.以“Over Current Context”的呈现样式模态呈现的 ViewController 在被解除时不会在呈现的 VC 中触发ViewWillAppear The answer below worked fine as a hack, but the easier solution is what I was looking for.下面的答案作为一个 hack 工作得很好,但更简单的解决方案是我正在寻找的。 That is, changing the presentation style to Full Screen.即,将演示样式更改为全屏。


So I ended up implementing a compact solution - some will say that it's not OOP without the protocol, and I agree - but in the scope of this app, it solves the problem in the context of a simple present/dismiss construct.所以我最终实现了一个紧凑的解决方案 - 有些人会说它不是没有协议的 OOP,我同意 - 但在这个应用程序的范围内,它在一个简单的存在/解除结构的上下文中解决了问题。

In the presenting vc, I added this code.在演示 vc 中,我添加了此代码。

let vc = storyboard.instantiateViewController(withIdentifier: "DetailView") as! DetailViewController
vc.masterVC = self
self.present(vc, animated: true, completion: nil)

The declared a function in the masterVC to be called when the modal dismisses ie:当模态关闭时在 masterVC 中声明的一个函数将被调用,即:

func calledWhenModalDismisses() {
   // Do This
}

In the modal DetailView that pops up, I just create a member pointing to the master:在弹出的模式 DetailView 中,我只是创建了一个指向 master 的成员:

var masterVC: MasterViewController?

And then in my detail dismissal function, I can call back to the function I created in the MasterVC.然后在我的细节解除函数中,我可以回调我在 MasterVC 中创建的函数。

masterVC?.calledWhenModalDismisses()
dismiss(animated: true, completion: nil)

Again, not a truly OOP solution (without the protocol) but I'm sure this will help someone with a simple compact solution to a simple master/detail issue when the presenting vc needs to be alerted as the detail vc is being dismissed.同样,这不是一个真正的 OOP 解决方案(没有协议),但我相信这将帮助一个简单的紧凑解决方案的人解决一个简单的主/细节问题,当呈现的 vc 需要被警告时,因为细节 vc 被解雇了。 Apple should actually have a delegate function to handle this when a vc comes into "focus" but they do not.当 vc 进入“焦点”时,Apple 实际上应该有一个委托函数来处理这个问题,但他们没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM