无法使用setSelectedIndex关闭模态显示的视图

时间:2013-12-04 18:03:50

标签: ios objective-c uiviewcontroller uitabbarcontroller modalviewcontroller

我今天正在寻找有关我的应用程序所面临的问题的一些见解。

我有一个基于标签栏的应用程序(3个选项卡)。在选项卡#2中,这是事件序列。

  1. 在“选项卡#2”的rootView中单击“添加”按钮。
  2. 带有navBar的表格视图(PresentViewController)。
  3. Table Row触及。
  4. 新的UINavigationController提供了rootViewController,显示了一些图像
  5. 选择了其中一张图片
  6. 使用带有图像和详细信息的navBar(presentViewController)以模态方式呈现新视图。 7点击图片后,我想回到标签#2的根视图。(我正在使用代表)
  7. 我的问题是 =>除非我解雇每个模态视图,否则我无法转到标签#2。 =>如果我正在使用委托并在委托中设置selectedTabIndex = 1,则应显示选项卡#2的rootViewController。 =>但实际上,直到我解雇两个模态视图控制器才会发生。那是为什么?

    我在苹果文档中读到,如果您关闭模态视图控制器,那么所有后续视图控制器也将被解除。然而,情况似乎并非如此。只有在我解除每个模态视图后,我才能回到选项卡#2的根视图控制器。

    //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 个答案:

没有答案