Milos Pesic
Milos Pesic

Reputation: 720

Callback method for presentViewController

I'm having trouble with presentViewController method and its last parameter.

[self presentViewController:navigationController animated:YES completion:nil];

I'm new in objective-c syntax and can't find out what object should I pass to 'completion' parameter. (also didn't find any examples that use it)

I want to have callback method when my presented View Controller dismisses.

Thanks,

Milos

Upvotes: 4

Views: 27643

Answers (4)

Jan Ehrhardt
Jan Ehrhardt

Reputation: 405

For anyone still reading this after all these years, I wanted to alert the user when an error happened inside a UIImagePickerController. Solution

if (error condition) {
    [self dismissViewControllerAnimated:YES completion:^(void) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // Alert the user here, for instance with a UIAlertController
        });
    }];
}

Upvotes: 0

Aymarick
Aymarick

Reputation: 533

Example of creating a completion block:

[self presentViewController:navigationController 
                   animated:YES 
                 completion:^(){
                     //put your code here
                 }];

This block does not take parameters. other blocks may take parameters and you can define them like this example:

^(BOOL bFinished){
    //put your code here
}

Upvotes: 17

jrturton
jrturton

Reputation: 119272

This method isn't going to give you what you want. The completion block is for when the view controller has completed presenting, not when it is dismissed. You'll need to use a different pattern, such as delegation, to get a callback when the controller is dismissed.

The completion handler is called after the viewDidAppear: method is called on the presented view controller.

See presentViewController:animated:completion:

Upvotes: 11

Alexander
Alexander

Reputation: 8147

That's a place for a block.

The completion handler is called after the viewDidAppear: method is called on the presented view controller.

For operations on dismissal you could place your code in the viewWillDisappear: or viewDidDisappear: method.

Upvotes: 7

Related Questions