2

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);

            });
mcfly soft
  • 11,289
  • 26
  • 98
  • 202
  • that means your self.parentViewController is not TextViewerViewController, can you print out ParentViewController variable? – Özgür Ersil Jun 11 '15 at 13:48
  • With notification [this will help you][1] or with delegate [try this][2] [1]: http://stackoverflow.com/a/30274432/2771713 [2]: http://stackoverflow.com/a/6169104/2771713 – bLacK hoLE Jun 11 '15 at 13:54
  • Thanks a lot for helping ! I just found the solution with your helps. See Update 2. – mcfly soft Jun 11 '15 at 14:01

0 Answers0