Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I present a ModalViewController from a UIActivity item picked from UIActivity View Controller?

I am working on an app that presents some data in a detailViewController. I have a rightBarButton in the navbar that presents a UIActivityViewController that is filled with my own UIActivity sublclassed items. Most of them work fine as they are just changing a small aspect to the data from the detail view, but I need one of them to open a modalViewController when selected. I keep getting the following warning from the console.....

    Warning: Attempt to present <UINavigationController: 0x1fd00590>  
on <UITabBarController: 0x1fde1070> which is already presenting <MPActivityViewController: 0x1fd2f970>

I suppose it is worth noting that the app doesn't crash but the modal view doesn't appear either. I am assuming that the UIActivityViewController is a modal view itself and you can only display one of those at a time so the task is to figure out how to perform my segue after the ActivityView has disappeared, but thats where I am stumped. I welcome any help, thoughts or feedback. I've tried google but haven't had much luck, I assume because The UIActivityViewController is so new.

Here is my setup so far, my UIActivity objects have a delegate set to the detailViewController for a custom protocol that lets the detailViewController perform the data changes and then update its view.

for the activities in question that should present the modalView controller I have tried a few approaches that all get the same warning.


None of These Works!!!


1) simply tried performing segue from my delegate method

- (void) activityDidRequestTransactionEdit
{
    NSLog(@"activityDidRequestTransactionEdit");
    [self performSegueWithIdentifier:@"editTransaction" sender:self];
}

2)tried setting a completion block on the UIActivityViewController and having my delegate method set a bool flag that the modal view should be shown(self.editor)

[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed) {
    NSLog(@"completed dialog - activity: %@ - finished flag: %d", activityType, completed);
    if (completed && self.editor) {
        [self performSegueWithIdentifier:@"editTransaction" sender:self];
    }
}];

3) subclassing the UIActivityViewController itself, giving it the detailView as a delegate, and overriding it's dismissViewControllerAnimated: method with my own completion block

- (void) dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
    [super dismissViewControllerAnimated:flag completion:^{
        [self.MPActivityDelegate activityDidRequestTransactionEdit];

    }];
}

The working Solution


In the UIActivity subclass you need to override this method like so

- (UIViewController *) activityViewController {
    MPEditMyDataViewController *controller = [[MPEditMyDataViewController alloc] init];
    controller.activity = self; // more on this property below
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
    return navController;
}

in your MPEditMyDataViewController.h (the view controller the chosen action should produce) You need a property back to the activity subclass like so

@property (strong, nonatomic) MPEditMyDataActivity *activity;

in your MPEditMyDataViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
                                     initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                     target:self
                                     action:@selector(cancel)];

    self.navigationItem.leftBarButtonItem = cancelButton;
}
// here's how you dismiss the view controller when you are done with it
// after saving the changes to your data or whatever the view controller is supposed to do.
-(void) cancel
{
    NSLog(@"Cancel Button Pushed");
    [self.activity activityDidFinish:YES];
}
@end
like image 245
vichudson1 Avatar asked Mar 12 '13 19:03

vichudson1


1 Answers

Did some more documentation digging and found this method for UIActivity subclassing

- (UIViewController *) activityViewController

it gets my view controller to pop up like I wanted by returning it from here instead of trying to segue it from my detailViewController. Now to figure out how to dismiss it when I'm done with it!!!!

like image 88
vichudson1 Avatar answered Oct 04 '22 17:10

vichudson1