从子 UIViewController 调用父 UIViewController 方法
calling a parent UIViewController method from a child UIViewController
我有一个父 UIViewController,它打开一个子 UIViewController:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("myChildView") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
我在 ChildView 中按下一个按钮,它应该关闭 ChildView 并在父视图中调用一个方法:
self.dismissViewControllerAnimated(true, completion: nil)
CALL PARENTS METHOD ??????
怎么做?
我找到了一个很好的答案 (),但我不确定这是否是 UIViewControllers 的最佳实践。有人可以帮忙吗?
我在你的问题中注意到了一些事情:在关闭视图控制器后不要调用方法(在你的情况下是父方法)。关闭视图控制器将导致它被释放。后面的命令可能不会执行。
您在问题中包含的 link 指向一个很好的答案。在你的情况下,我会使用委托。在关闭子视图控制器之前调用父视图控制器中的委托方法。
这是一个很棒的tutorial。
向应该包含对父视图控制器的引用的子视图控制器添加弱属性
class ChildViewController: UIViewController {
weak var parentViewController: UIViewController?
....
}
然后在你的代码中(我假设它在你的父视图控制器中),
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("myChildView") as! ChildViewController
vc.parentViewController = self
self.presentViewController(vc, animated: true, completion: nil)
并且,正如 vomako 所说,在关闭子视图控制器之前调用父方法。
parentViewController.someMethod()
self.dismissViewControllerAnimated(true, completion: nil)
或者,您也可以在 dismissViewControllerAnimated 的完成参数中调用该方法,它将在 运行 after you child view controller dismisses:
self.dismissViewControllerAnimated(true) {
parentViewController.someMethod()
}
实现此目的的一种简单方法是使用 NSNotificationCenter
。
在您的 ParentViewController
中将此添加到 viewDidLoad
方法中:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: Selector(("refresh:")), name:NSNotification.Name(rawValue: "refresh"), object: nil)
}
之后在 ParentViewController
中添加此函数,当您关闭 ChildViewController
:
时将调用该函数
func refreshList(notification: NSNotification){
print("parent method is called")
}
并在您的 ChildViewController
中添加此代码以关闭您的子视图:
@IBAction func btnPressed(sender: AnyObject) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refresh"), object: nil)
self.dismiss(animated: true, completion: nil)
}
现在,当您关闭子视图时,将调用 refreshList
方法。
protocol SampleProtocol
{
func someMethod()
}
class parentViewController: SampleProtocol
{
// Conforming to SampleProtocol
func someMethod() {
}
}
class ChildViewController
{
var delegate:SampleProtocol?
}
self.dismissViewControllerAnimated(true, completion: nil)
delegate?.someMethod()
我有一个父 UIViewController,它打开一个子 UIViewController:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("myChildView") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
我在 ChildView 中按下一个按钮,它应该关闭 ChildView 并在父视图中调用一个方法:
self.dismissViewControllerAnimated(true, completion: nil)
CALL PARENTS METHOD ??????
怎么做?
我找到了一个很好的答案 (
我在你的问题中注意到了一些事情:在关闭视图控制器后不要调用方法(在你的情况下是父方法)。关闭视图控制器将导致它被释放。后面的命令可能不会执行。
您在问题中包含的 link 指向一个很好的答案。在你的情况下,我会使用委托。在关闭子视图控制器之前调用父视图控制器中的委托方法。
这是一个很棒的tutorial。
向应该包含对父视图控制器的引用的子视图控制器添加弱属性
class ChildViewController: UIViewController {
weak var parentViewController: UIViewController?
....
}
然后在你的代码中(我假设它在你的父视图控制器中),
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("myChildView") as! ChildViewController
vc.parentViewController = self
self.presentViewController(vc, animated: true, completion: nil)
并且,正如 vomako 所说,在关闭子视图控制器之前调用父方法。
parentViewController.someMethod()
self.dismissViewControllerAnimated(true, completion: nil)
或者,您也可以在 dismissViewControllerAnimated 的完成参数中调用该方法,它将在 运行 after you child view controller dismisses:
self.dismissViewControllerAnimated(true) {
parentViewController.someMethod()
}
实现此目的的一种简单方法是使用 NSNotificationCenter
。
在您的 ParentViewController
中将此添加到 viewDidLoad
方法中:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: Selector(("refresh:")), name:NSNotification.Name(rawValue: "refresh"), object: nil)
}
之后在 ParentViewController
中添加此函数,当您关闭 ChildViewController
:
func refreshList(notification: NSNotification){
print("parent method is called")
}
并在您的 ChildViewController
中添加此代码以关闭您的子视图:
@IBAction func btnPressed(sender: AnyObject) {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refresh"), object: nil)
self.dismiss(animated: true, completion: nil)
}
现在,当您关闭子视图时,将调用 refreshList
方法。
protocol SampleProtocol
{
func someMethod()
}
class parentViewController: SampleProtocol
{
// Conforming to SampleProtocol
func someMethod() {
}
}
class ChildViewController
{
var delegate:SampleProtocol?
}
self.dismissViewControllerAnimated(true, completion: nil)
delegate?.someMethod()