mcfly soft
mcfly soft

Reputation: 11651

viewdidload called after closing dismissing child viewcontroller

I have a main Viewcontroller and a Child Viewcontroller.

I realized that when closing the Childviewcontroller like:

        self.dismissViewControllerAnimated(true, completion: {
            self.dismissViewControllerAnimated(true, completion: nil);
        });

it will just dismiss the child viewcontroller and I can see the mainview controller. No other code is processed

if I close the child viewcontroller with a notification to the main viewcontroller :

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

                });

then my refreshtextviewer_with_bookmark() function is called in the main viewcontroller and in parallel the standard viewDidLoad() is also called.

Is it normal behaviour, that in this case the viewDidLoad() is called after dismissing the child viewcontroller ? Is there a way to prevent this ?

Upvotes: 2

Views: 2030

Answers (1)

rshev
rshev

Reputation: 4176

Shouldn't be called after dismissing. Tested, and not called indeed.

Here's the quick example:

PARENT

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        println(__FUNCTION__)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshtextviewer_with_bookmark", name: "refreshtextviewer_with_bookmark", object: nil)
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    @IBAction func buttonTap(sender: AnyObject) {
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("childVC") as! ChildViewController
        self.presentViewController(vc, animated: true, completion: nil)
    }

    func refreshtextviewer_with_bookmark() {
        println(__FUNCTION__)
    }
}

CHILD

class ChildViewController: UIViewController {

    @IBAction func exitTap(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: { () -> Void in
            NSNotificationCenter.defaultCenter().postNotificationName("refreshtextviewer_with_bookmark", object: nil)
        })
    }
}

FYI __FUNCTION__ macro is replaced with the function name.

Upvotes: 2

Related Questions