Question

I have a HomeView and a HomeDropDownView.

HomeDropDownView is shown as a drop-down view over the HomeView.

HomeView is a delegate of HomeDropDownView.

When I do an action in HomeDropDownView I want to call a delegate method in HomeView and have that delegate method present a third view controller, TestViewController from it's navigation controller.

If I try to launch TestViewController from anywhere in the class it works fine - except from the delegate method.

There are animations in HomeDropDownView but putting the call to the delegate method in the complition does not make the view controller appear. And in the case that I'm using this the animation's don't fire anyway; there's only a resizing without animation.

TestViewController's init does get called as well as the viewDidLoad but not the viewWillAppear and the view dose not appear.

Code:

HomeDropDownView

- (void)finalAction {
    ...
    [self callDelegateAction];
    ...

- (void)calldelegateAction {
    if ([self.delegate respondsToSelector:@selector(launchTestView)] ) {
        [self.delegate launchTestView];
    } else {
        DLog(@"Error out to the user.");
    }
}

HomeView

- (void)launchTestView {
        //[self listSubviewsOfView:self.parentViewController.view];

        NSLog(@"delegate method | self: %@", self);

        TestViewController *tvc = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
        //[self.navigationController presentViewController:tvc animated:YES completion:nil];
        //[self.view.window.rootViewController presentViewController:tvc animated:YES completion:nil];
        //[self.navigationController pushViewController:tvc animated:YES];
        AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appdelegate.tabBarController.navigationController presentViewController:tvc animated:YES completion:^() {
            NSLog(@"Done!");
        }];
}

None of the above approaches work. But if I put the exact same code into the viewDidAppear or put it in a button action method, it will work fine. At the time of calling the delegate method's self is HomeView and all the subviews, including the nav controller do seem to be there. This is in a tabcontroller-based project but I think that any of the above are acceptable ways to call the nav controller still.

What am I missing? Why does my delegate method not want to push/present a viewcontroller on HomeView's Nav controller? It's probably something I'm missing but I can't find a reason in the Apple Docs or any other thread. Thanks for the help!

Was it helpful?

Solution

Sadly this turned out to be that HomeView was being changed underneath the execution of the message. So by the time the HomeView got the message call it was no longer the same HomeView object that had requested action in the first place. So it was not the same delegate. This was done so that it would appear to the user that the same view was being used for different things. But this is a good example of why you should not destroy and re-create critical views. We should have been using the same view and reloading the objects instead if we knew that we would be sending messages. Or had some notion of a control structure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top