简体   繁体   English

从子 UIViewController 调用父 UIViewController 方法

[英]calling a parent UIViewController method from a child UIViewController

I have a Parent UIViewController, which opens 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)

I press a Button in the ChildView which should close the the ChildView and call a method in the Parent View:我按下 ChildView 中的一个按钮,它应该关闭 ChildView 并在父视图中调用一个方法:

self.dismissViewControllerAnimated(true, completion: nil)
CALL PARENTS METHOD  ??????

How to do that ?怎么做 ? I found a good answer ( Link to good answer ), but I am not sure if this is the best practice with UIViewControllers.我找到了一个很好的答案( 链接到好的答案),但我不确定这是否是 UIViewControllers 的最佳实践。 Can someone help ?有人可以帮忙吗?

One easy way to achieve this you can use NSNotificationCenter for that.实现此目的的一种简单方法是使用NSNotificationCenter

In your ParentViewController add this into viewDidLoad method:在您的ParentViewController添加到viewDidLoad方法中:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: Selector(("refresh:")), name:NSNotification.Name(rawValue: "refresh"), object: nil)
}

After that add this function in ParentViewController which will get called when you dismiss your ChildViewController :之后在ParentViewController中添加这个函数,当你关闭你的ChildViewController时它会被调用:

func refreshList(notification: NSNotification){

    print("parent method is called")
}

and into your ChildViewController add this code where you dismissing your child view:并在您的ChildViewController添加此代码,您可以在其中关闭子视图:

 @IBAction func btnPressed(sender: AnyObject) {

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refresh"), object: nil)
    self.dismiss(animated: true, completion: nil)
}

Now when you dismiss child view refreshList method will be call.现在,当您关闭子视图时,将调用refreshList方法。

Add a weak property to the child view controller that should contain a reference to the parent view controller向子视图控制器添加一个弱属性,该属性应包含对父视图控制器的引用

class ChildViewController: UIViewController {
weak var parentViewController: UIViewController?
....
}

Then in your code (I'm assuming it's inside your parent view controller),然后在你的代码中(我假设它在你的父视图控制器中),

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("myChildView") as! ChildViewController
vc.parentViewController = self
self.presentViewController(vc, animated: true, completion: nil)

And, as vomako said, call the parent's method before dismissing your child view controller.而且,正如 vomako 所说,在解除子视图控制器之前调用父方法。

parentViewController.someMethod()
self.dismissViewControllerAnimated(true, completion: nil)

Or, you can also call the method in the completion paramter of dismissViewControllerAnimated, where it will be run after you child view controller dismisses:或者,您也可以在dismissViewControllerAnimated的完成参数中调用该方法,它会您的子视图控制器关闭运行:

self.dismissViewControllerAnimated(true) {
    parentViewController.someMethod()
}

Something that I've noticed in your question: Do not call a method (the parent method in your case) after dismissing the view controller.我在您的问题中注意到的一些事情:在解除视图控制器后不要调用方法(在您的情况下为父方法)。 Dismissing the view controller will cause it to be deallocated.关闭视图控制器将导致它被释放。 Later commands may not be executed.以后的命令可能无法执行。

The link that you've included in your question points to a good answer.您在问题中包含的链接指向了一个很好的答案。 In your case, I would use delegation.在你的情况下,我会使用委托。 Call the delegation method in the parent view controller before you dismiss the child view controller.在关闭子视图控制器之前调用父视图控制器中的委托方法。

Here is an excellent tutorial .这是一个很好的教程

protocol SampleProtocol 
{
    func someMethod()
}   


class parentViewController: SampleProtocol 
{
    // Conforming to SampleProtocol
    func someMethod() {
    }
}

 class ChildViewController
    {
        var delegate:SampleProtocol?
    }

    self.dismissViewControllerAnimated(true, completion: nil)
    delegate?.someMethod()

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

相关问题 有没有办法从父UIViewController调用子UIViewController方法? - Is there a way To call a Child UIViewController method from Parent UIViewController? 从不同的UIViewController调用方法 - Calling method from different UIViewController 通过addSubview添加后,父UIViewController中的调用方法 - Calling method in parent UIViewController, after adding by addSubview 如何从子UIViewController转到父UIViewController,再从那里到另一个子UIViewController - How to go from child UIViewController to parent UIViewController and from there to another child UIViewController 在UIViewController中调用委托方法 - Calling delegate method in UIViewController 是否UIViewController调用方法presentViewController从内存中删除? - Is the UIViewController calling the method presentViewController removed from the memory? 以异步方式从UIViewController调用方法presentViewController - Calling method presentViewController from the UIViewController in an asynchronous way 从包含的对象中调用UIViewController方法 - Calling UIViewController method from within contained object uiscrollview中的uibutton中的父uiviewcontroller中的触发方法 - Trigger method in parent uiviewcontroller from uibutton in uiscrollview 从UIViewcontroller调用storyboard - Calling storyboard from UIViewcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM