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

Ios 无法使用setSelectedIndex关闭模式显示的视图,ios,objective-c,uiviewcontroller,uitabbarcontroller,modalviewcontroller,Ios,Objective C,Uiviewcontroller,Uitabbarcontroller,Modalviewcontroller,今天我想了解一下我的应用程序所面临的问题 我有一个基于选项卡栏的应用程序(3个选项卡)。在表2中,以下是事件的顺序 在选项卡2的根视图中单击“添加”按钮 以模式显示导航栏的表格视图(PresentViewController) 桌子一排被碰了一下 新的UINavigationController与rootViewController一起提供,显示了一些图像 选择其中一个图像 使用带有图像和详细信息的导航栏(presentViewController)以模式显示新视图。 7单击图像后,我想返回选项

今天我想了解一下我的应用程序所面临的问题

我有一个基于选项卡栏的应用程序(3个选项卡)。在表2中,以下是事件的顺序

  • 在选项卡2的根视图中单击“添加”按钮
  • 以模式显示导航栏的表格视图(PresentViewController)
  • 桌子一排被碰了一下
  • 新的UINavigationController与rootViewController一起提供,显示了一些图像
  • 选择其中一个图像
  • 使用带有图像和详细信息的导航栏(presentViewController)以模式显示新视图。 7单击图像后,我想返回选项卡2的根视图。(我正在为此使用代理)
  • 我的问题是 =>除非我关闭每个模式视图,否则我无法转到选项卡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
    }
    

    密码。展示你如何拒绝并表达你的观点。你在用故事板吗?谢谢回复@CaptJak。我已经添加了代码,希望它能澄清我的问题。