问题
I am presenting an SLComposeViewController
to post to Facebook in my app. The user is able to dismiss this View Controller in one of two ways: either by posting their post to Facebook, or by pressing "cancel". When the user presses "cancel", the SLComposeViewController
is dismissed, and the user is returned to the presenting View Controller that is behind it.
However, what I would like to do is if the user presses "post", then I want the presenting View Controller to ALSO be dismissed after the SLComposeViewController
is dismissed (i.e. in the SLComposeViewControllerResultDone
case). My problem is that I am not sure how to do this. I realize that I would use the completion handler for this, but I am stuck here. Here is the code that I have which presents the SLComposeViewController
:
SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbSheet setInitialText:initialText];
[fbSheet addImage:myImage];
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result) {
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(@"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(@"Posted....");
}
break;
}
};
[fbSheet setCompletionHandler:completionHandler];
[self presentViewController:fbSheet animated:YES completion:nil];
With the completion handler above, I get the NSLog
outputs as expected. However,
Can anyone see what it is I'm doing wrong? As I've pointed out, I need the dismissal of the presenting View Controller to occur ONLY if the user "posts" to Facebook, but NOT when they cancel.
回答1:
You could simply dismiss the presenting view controller in SLComposeViewControllerResultDone
part of completionHandler as below:
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
If you are supporting iOS 6, then you need to dismiss the SLComposeViewController
first.
回答2:
you don't need to dismiss ViewController in the completion handler, it will be handled when you press cancel button
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController * fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:initialText];
[fbSheet addImage:myImage];
[self presentViewController:controller animated:YES completion:Nil];
}
来源:https://stackoverflow.com/questions/27664399/trying-to-dismiss-view-controller-that-presents-slcomposeviewcontroller-after-us