calling parents viewcontroller method when dismissing child viewcontroller

2.5k Views Asked by At

I would like to call a method of my parent view controller from my child view controller as follow: (I already tried to do the same with notification, but it did not help in my case, so I tried a new way like the following)

     var ParentViewController = self.parentViewController as! TextViewerViewController;
     self.dismissViewControllerAnimated(true, completion: {
          ParentViewController.refreshtextviewer_with_bookmark();
     });

My TextViewerViewController has a method:

func refreshtextviewer_with_bookmark(){

With this I get a "fatal error: unexpectedly found nil while unwrapping an Optional value" at the code :

var ParentViewController=self.parentViewController as! TextViewerViewController;

Can anybode help in this ?

UPDATE on why I would like to try this approach:

My parents method refreshtextviewer_with_bookmark is a longrunning operation and I want to show the user the progress, which works. With my already implemented approach with notifications the user sees the childview as long the process of my parents method is running. Not before this is finished the parentview is showed. Thats why I am looking for another approach and thought this would be the right one. I guess I need a solution that my method is called after the childview is already dismissed.

UPDATE 2 Solution found for me, with hints of comments: I simply call the notification in the completion block:

self.dismissViewControllerAnimated(true, completion: {

NSNotificationCenter.defaultCenter().postNotificationName("refreshtextviewer_with_bookmark", object: nil);
                self.dismissViewControllerAnimated(true, completion: nil);

            });
0

There are 0 best solutions below