views:

186

answers:

1

I am having trouble programmatically removing stacks from a view. I am doing things a bit convoluted, which is certainly not helping matters. Here is what I want to do:

  1. User is in one tab of a tab view controller.
  2. User selects item from table that contains URL.
  3. App switches to another tab view, and sets an existing UIWebView to the selected URL.
  4. Any views that are on the UIWebView's stack are popped off, showing the UIWebView.

I have up to step three working fine. But, can not seem to pop any views off the stack. Here is my attempt:

- (void)openAndDisplayURL:(NSString*)URL {
tabBarController.selectedIndex = 0;

UIViewController *selectedController = tabBarController.selectedViewController;

if ([selectedController isKindOfClass:[UINavigationController class]]) {
    UINavigationController *controller = (UINavigationController*)selectedController;
    NSArray *views = controller.viewControllers;
    for (id view in views) {
        if([view respondsToSelector:@selector(openURLWithString:)]) {
            NSString *completeURL = [NSString stringWithFormat:@"http://%@",URL];
            [view openURLWithString:completeURL];
        } else if ([selectedController isKindOfClass:[UINavigationController class]]) {
            UINavigationController *subcontroller = (UINavigationController*)selectedController;

            [subcontroller.navigationController popViewControllerAnimated:NO];

        }
    }
}
}

Debugging shows the expected number of views, of the type expected. It properly enters the else block when it's a view that needs to be removed, but calling popViewControllerAnimated: does nothing.

Any help would be appreciated.

+2  A: 

You should call UINavigationController popToViewController method.

popToViewController:animated:

Pops view controllers until the specified view controller is at the top of the navigation stack.

  • (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated Parameters

viewController

The view controller that you want to be at the top of the stack.

animated

Set this value to YES to animate the transition. Pass NO if you are

setting up a navigation controller before its view is displayed.

Return Value

An array containing the view controllers that were popped from the stack. Discussion

For information on how the navigation bar is updated, see “Updating the Navigation Bar.”

Dougnukem