简体   繁体   中英

Navigate between nested View Controllers across a TabBarController - Xamarin iOS

I'm having trouble with navigation outside of the typical use cases for navigation controllers and tabbar controllers. Here's a drawing of a simplified version. (real version has a TabBarController that feeds into 9 NavControllers)

AppNavigation

In my Home Nav Controller, there's a table view where the user could select a row and that should navigate them to a particular Events Detail View Controller. At that point, the user should be able to navigate back to the Events TableView Controller to see the list of events or use the TabBar to navigate to another section.

So far with my code I can push the correct detail view controller and select the correct index of the tabbar but it only works the first time:

 public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
     {
         tableView.DeselectRow(indexPath, true);

         var eventsDetailView = new EventsDetailController(eventPosts[indexPath.Row]);
         //loop through ViewControllers array in case the user has set a custom tabbar order via the More Tab  
         foreach (UIViewController viewC in tbController.ViewControllers)
         {
             UINavigationController navController = viewC as UINavigationController;

             if (navController.TabBarItem.Tag.Equals(9))
             {
                 navController.PushViewController(eventsDetailView, false);       
                 tbController.SelectedIndex = 9;
             }                  
         }
     }

After the user has navigated to the events detail view controller, if they then:

  1. travel to the Home ViewController via the TabBar

  2. select a new TableRow to navigate to a different Events detail view controller ... then the same previous events detail view shows up. It doesn't matter which row the user selects in the Home View Controller. The initial detail view controller will always be the same one to be presented.

Any help pointing me in the right direction would be greatly appreciated!

Solution:

In my understanding, what you want is :

HomePageController --> EventDetailPage --> EvevtTableViewControll

So in the first step, you could push to EventDetailPage in HomePageController :

this.NavigationController.PushViewController(new HomeDetailUIViewController(), true);

Then, in your EventDetailPage , you can customize a back button, for example:

   UIBarButtonItem backBtn = new UIBarButtonItem();
   backBtn.Title = "back";
   backBtn.Style = UIBarButtonItemStyle.Plain;
   this.NavigationItem.LeftBarButtonItem = backBtn;

   backBtn.Clicked += (sender, e) =>
   {
       // 1 here is an example, it should be the index of your EvevtTableViewControll
       NavigationController.TabBarController.SelectedIndex = 1;
       NavigationController.PopToRootViewController(true);
   };

Set the NavigationController.TabBarController.SelectedIndex=1 first to make sure EvevtTableViewControll will show after you back from EvevtDetailViewControll , then your PopToRootViewController to back to the EvevtTableViewControll .

Try it and let me know if you have any problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM