Cannot dismiss modally presented views by using setSelectedIndex

232 Views Asked by At

I am looking today for some insight into the issue that I am facing with my app.

I have a tab bar based application (3 tabs). In tab #2, here's the sequence of events.

  1. "Add" button clicked in the rootView of tab #2.
  2. Table View with navBar presented modally (PresentViewController).
  3. Table Row touched.
  4. New UINavigationController presented with it's rootViewController showing a few images
  5. One of the images is selected
  6. Present new view modally with navBar (presentViewController) with Image and details. 7 Upon clicking the image, I want to go back to the root view of tab # 2. (and I am using delegates for this)

My problem is that => Unless I dismiss each and every modal view, I am unable to go to the tab# 2. => If I am using delegate and setting the selectedTabIndex = 1 in the delegate the rootViewController of tab #2 should get displayed. => But In actuality that doesn't happen till i dismiss both modal view controllers. Why is that?

I read in the apple documentation that if you dismiss a modal view controller then all the subsequent view controllers would be dismissed as well. This however does not seem to be the case. I can get back to the root view controller of tab#2 only after I dismiss each and every modal view.

//AppDelegate :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.tabBarController      = [[UITabBarController alloc] init];
    self.secondViewController  = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    self.navControllerTwo      = [[UINavigationController alloc] initWithRootViewController:self.secondViewController];
    //navControllerOne and viewControllerOne are also init'd here
    NSArray *viewControllers   = [NSArray arrayWithObjects:self.navControllerOne, self.navControllerTwo, self.viewControllerOne, nil];
    [self.tabBarController setViewControllers:viewControllers];
    self.window.rootViewController  = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

// Then => 1. "Add" button clicked in the rootView of tab #2.

- (void)addItemButtonClicked:(id)sender
{
    UIBarButtonItem *btn = nil;
    AddItemViewcontroller *addItemController = [[AddItemViewcontroller alloc] initWithNibName:nil bundle:nil];
    addItemController.delegate = self;
    UINavigationController *addItemNavController = [[UINavigationController alloc] initWithRootViewController:addItemController];
    //leftBaButton
    btn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                           style:UIBarButtonItemStylePlain
                                          target:self
                                          action:@selector(addItemCancelButtonPressed:)];
    addItemNavController.navigationBar.topItem.leftBarButtonItem  = btn;
    [self presentViewController:addItemNavController animated:YES completion:nil]; // First Modal View presented
}

//Then: => 2. Table View with navBar presented modally (PresentViewController).
//         3. Table Row touched.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    IGViewController *gvc = [[IGViewController alloc] initWithItemType:itemType]; // pre-determined item type
   [self. navigationController pushViewController:gvc animated:YES];
}

//Then: 4. New UINavigationController presented with it's rootViewController showing a few images
//      5. One of the images is selected

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
{
    UIBarButtonItem *btn;
    UINavigationItem *navItem = [[UINavigationItem alloc] init];
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];

    SIViewController *siViewController = [[SIViewController alloc] initWithNibName:nil bundle:nil];
    siViewController.delegate = self.appDelegate.secondViewController; // Init'd in the app delegate
    btn = [[UIBarButtonItem alloc] initWithTitle:@"Add item" style:UIBarButtonItemStyleDone
                                          target:selectedItemViewController
                                          action:@selector(selectedItemViewAddItemButtonPressed)];
    [navItem setLeftBarButtonItem:btn];
    navBar.items = [NSArray arrayWithObject:navItem];
    [navBar setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    [selectedItemViewController.view addSubview:navBar];
    [self presentViewController:selectedItemViewController animated:YES completion:nil]; //second modal view presentd here
}

//Then: 6. Present new view modally with navBar (presentViewController) with Image and details.
//     7 Upon clicking the image, I want to go back to the root view of tab # 2. (and I am using delegates for this)

-(void)siViewAddItemButtonPressed
{
    [self.delegate selectedItemViewAddItemButtonPressedForItem:self.selectedItem];
}

//And: Here's what the method in the siViewControllers delegate looks like:
//In this routine setSelectedIndex = 2 does not work until i dismiss both modally presented views

- (void)siViewAddItemButtonPressedForItem:(Item *)item
{
    [self.presentedViewController dismissViewControllerAnimated:NO completion:nil]; // Dismiss modal
    [(UINavigationController*)(self.presentedViewController) popToRootViewControllerAnimated:NO];
    [self.presentedViewController dismissViewControllerAnimated:NO completion:nil]; //Dismiss modal
}
0

There are 0 best solutions below