xcode Swift:在关闭模态后在 ViewController 上执行一个功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27476965/
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
Swift: Perform a function on ViewController after dismissing modal
提问by Michael Voccola
A user is in a view controller which calls a modal. When self.dismissViewController is called on the modal, a function needs to be run on the initial view controller. This function also requires a variable passed from the modal.
用户在调用模态的视图控制器中。当在模态上调用 self.dismissViewController 时,需要在初始视图控制器上运行一个函数。此函数还需要从模态传递的变量。
This modal can be displayed from a number of view controllers, so the function cannot be directly called in a viewDidDisappear on the modal view.
该模态可以从多个视图控制器中显示,因此不能在模态视图上的 viewDidDisappear 中直接调用该函数。
How can this be accomplished in swift?
这如何快速完成?
采纳答案by Pikaurd
How about delegate
?
怎么样delegate
?
Or you can make a ViewController
like this:
或者你可以做一个ViewController
这样的:
typealias Action = (x: AnyObject) -> () // replace AnyObject to what you need
class ViewController: UIViewController {
func modalAction() -> Action {
return { [unowned self] x in
// the x is what you want to passed by the modal viewcontroller
// now you got it
}
}
}
And in modal:
在模态中:
class ModalViewController: UIViewController {
var callbackAction: Action?
override func viewDidDisappear(_ animated: Bool) {
let x = … // the x is what you pass to ViewController
callbackAction?(x)
}
}
Of course, when you show ModalViewController
need to set callbackAction
like this modal.callbackAction = modalAction()
in ViewController
当然,当你表现出ModalViewController
需要设置callbackAction
这样modal.callbackAction = modalAction()
的ViewController
回答by Dave G
The answer supplied and chosen by the question asker (Michael Voccola) didn't work for me, so I wanted to supply another answer option. His answer didn't work for me because viewDidAppear does not appear to run when I dismiss the modal view.
提问者(Michael Voccola)提供和选择的答案对我不起作用,所以我想提供另一个答案选项。他的回答对我不起作用,因为当我关闭模式视图时 viewDidAppear 似乎没有运行。
I have a table and a modal VC that appears and takes some table input. I had no trouble sending the initial VC the modal's new variable info. However, I was having trouble getting the table to automatically run a tableView.reloadData function upon dismissing the modal view.
我有一个表格和一个模态 VC,它出现并接受一些表格输入。我可以毫不费力地将初始 VC 发送到模态的新变量信息。但是,在关闭模态视图时,我无法让表自动运行 tableView.reloadData 函数。
The answer that worked for me was in the comments above:
对我有用的答案在上面的评论中:
You likely want to do this using an unwind segue on the modal, that way you can set up a function on the parent that gets called when it unwinds. stackoverflow.com/questions/12561735/… – porglezomp Dec 15 '14 at 3:41
您可能希望在模态上使用 unwind segue 来执行此操作,这样您就可以在父级上设置一个函数,该函数在它展开时被调用。stackoverflow.com/questions/12561735/... – porglezomp 2014 年 12 月 15 日 3:41
And if you're only unwinding one step (VC2 to VC1), you only need a snippet of the given answer:
如果您只展开一个步骤(VC2 到 VC1),您只需要给定答案的片段:
Step 1:Insert method in VC1 code
第一步:在VC1代码中插入方法
When you perform an unwind segue, you need to specify an action, which is an action method of the view controller you want to unwind to:
当你执行一个unwind segue时,你需要指定一个action,它是你想要unwind到的视图控制器的一个action方法:
@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
//Insert function to be run upon dismiss of VC2
}
Step 2:In storyboard, in the presented VC2, drag from the button to the exit icon and select "unwindToThisViewController"
步骤 2:在 storyboard 中,在呈现的 VC2 中,从按钮拖动到退出图标并选择“unwindToThisViewController”
After the action method has been added, you can define the unwind segue in the storyboard by control-dragging to the Exit icon.
添加 action 方法后,您可以通过按住 Control 拖动到 Exit 图标来定义故事板中的 unwind segue。
And that's it. Those two steps worked for me. Now when my modal view is dismissed, my table updates. Just figured I'd add this, in case anyone else's issue wasn't solved by the chosen answer.
就是这样。这两个步骤对我有用。现在,当我的模态视图被关闭时,我的表格会更新。只是想我会添加这个,以防其他人的问题没有被选择的答案解决。
回答by Michael Voccola
I was able to achieve the desired result by setting a Global Variable as a boolean value from the modal view controller. The variable is initiated and made available from a struct in a separate class.
通过将全局变量设置为来自模态视图控制器的布尔值,我能够实现所需的结果。该变量是从一个单独的类中的结构启动并提供的。
When the modal is dismissed, the viewDidAppear method on the initial view controller responds accordingly to the value of the global variable and, if needed, flips the value on the global variable.
当模式关闭时,初始视图控制器上的 viewDidAppear 方法会相应地响应全局变量的值,并在需要时翻转全局变量上的值。
I am not sure if this is the most efficient way from a performance perspective, but it works perfectly in my scenario.
从性能的角度来看,我不确定这是否是最有效的方式,但它在我的场景中完美运行。