0

ich eine Xamarin.iOS app, wo ich die folgende Struktur haben:Schwierigkeit mit TabBarControllers mit NavigationControllers vermischte

NavController (root: Login)

-> TabBarController (Home) - (Suche) - (Profil)

------> NavController (root: Home)

-------------> TableController

-----------------> DetailController

------> NavController (root: Search)

... etc

Ich habe zur Zeit Schwierigkeiten mit den Navigationspunkten zu arbeiten, speziell mit dem Zurück-Button.

Ich will nie wieder auf die Login-Seite über die Zurück-Taste gehen, also in meinem HomeController, blende ich die back button von

TabBarController.NavigationItem.HidesBackButton = true; 

sagen Als ich zum nächsten Bildschirm zu gehen (TableController) Ich mag siehe die zurück-Taste, die zurück zum HomeController geht, aber mein aktueller Ansatz hat eine zurück-Taste auf den Controller-Login

this.TabBarController.NavigationItem.HidesBackButton = false; 

Danke für jede Hilfe

+0

Verwenden Sie den Push-Modus, um den Login-View-Controller mit dem Tab-Bar-Controller zu verbinden? Wahrscheinlich können Sie versuchen, andere Segmente zu verwenden, damit sie sich nicht eine Navigationssteuerungsinstanz teilen. – Surely

Antwort

0

Für den gewünschten Effekt empfehle ich, ein Objekt zu erstellen (ich verwende "RootController" als Beispiel), um den Window.RootController der App zu verwalten.

Ich weiß nicht, ob Sie einige Erfahrung für RootController Wickel Fenster haben, so dass nur mein Schritt folgen.

zunächst ein neues Projekt erstellen, entfernen Sie das Storyboard und Viewcontroller.cs (Vergessen Sie nicht entfernen das Bündel für Storyboard im info.plist)

Dann wird Ihr AppDelegate.cs umschreiben, wie folgt aus:

[Register ("AppDelegate")] 
public class AppDelegate : UIApplicationDelegate 
{ 
    UIWindow window; 

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions) 
    { 
     // create a new window instance based on the screen size 
     window = new UIWindow (UIScreen.MainScreen.Bounds); 
     window.RootViewController = RootController.Instance.LoginController; 
     window.MakeKeyAndVisible(); 

     return true; 
    } 

    public override void OnResignActivation (UIApplication application) 
    { 
     // Invoked when the application is about to move from active to inactive state. 
     // This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) 
     // or when the user quits the application and it begins the transition to the background state. 
     // Games should use this method to pause the game. 
    } 

    public override void DidEnterBackground (UIApplication application) 
    { 
     // Use this method to release shared resources, save user data, invalidate timers and store the application state. 
     // If your application supports background exection this method is called instead of WillTerminate when the user quits. 
    } 

    public override void WillEnterForeground (UIApplication application) 
    { 
     // Called as part of the transiton from background to active state. 
     // Here you can undo many of the changes made on entering the background. 
    } 

    public override void OnActivated (UIApplication application) 
    { 
     // Restart any tasks that were paused (or not yet started) while the application was inactive. 
     // If the application was previously in the background, optionally refresh the user interface. 
    } 

    public override void WillTerminate (UIApplication application) 
    { 
     // Called when the application is about to terminate. Save data, if needed. See also DidEnterBackground. 
    } 
} 

und das ist RootController.cs, die eine UINavigationController und UITabBarController (Haus eingeschlossen - Suchen - Profil):

public class RootController 
{ 
    private static RootController instance; 
    public static RootController Instance { 
     get { 
      if (instance == null) 
       instance = new RootController(); 
      return instance; 
     } 
    } 

    private static UINavigationController loginController; 
    public UINavigationController LoginController { 
     get { 
      if (loginController == null) 
       InitLoginController(); 
      return loginController; 
     } 
    } 

    private void InitLoginController() 
    { 
     UIViewController loginViewController = new UIViewController(){ Title = "LoginController" }; 
     loginViewController.View.Frame = UIScreen.MainScreen.Bounds; 
     loginViewController.View.BackgroundColor = UIColor.Red; 
     loginViewController.NavigationItem.SetRightBarButtonItem (new UIBarButtonItem ("MainTab",UIBarButtonItemStyle.Done, delegate { 
      UIApplication.SharedApplication.KeyWindow.RootViewController = RootController.Instance.MainTabController; 
     }),true); 
     loginController = new UINavigationController (loginViewController); 
    } 

    private static UITabBarController mainTabController; 
    public UITabBarController MainTabController { 
     get { 
      if (mainTabController == null) 
       InitMainTabController(); 
      return mainTabController; 
     } 
     set { 
      mainTabController = value; 
     } 
    } 

    private void InitMainTabController() 
    { 
     mainTabController = new UITabBarController(); 
     mainTabController.ViewControllers = new UIViewController [] { 
      new UINavigationController(new HomeViewController() { 
       TabBarItem = new UITabBarItem (UITabBarSystemItem.Favorites,0) 
      }), 
      new UINavigationController (new UIViewController() 
       { 
        Title = "SearchController", 
        TabBarItem = new UITabBarItem (UITabBarSystemItem.Search,1) 
       }), 
      new UINavigationController (new UIViewController() 
       { 
        Title = "ProfileController", 
        TabBarItem = new UITabBarItem (UITabBarSystemItem.More,2) 
       }) 
     }; 

     mainTabController.SelectedIndex = 0; 
    } 
} 

Und das ist HomeController.cs die eine UITableViewController schieben können, wie Sie möchten, und hat auch eine Taste, um LoginController zurückzukehren, wenn Sie benötigt:

public class HomeViewController : UIViewController 
{ 
    public HomeViewController() 
    { 
     Title = "HomeController"; 
     this.View.Frame = UIScreen.MainScreen.Bounds; 
     this.View.BackgroundColor = UIColor.Green; 

     //Set a button to return to loginController 
     this.NavigationItem.SetLeftBarButtonItem (new UIBarButtonItem ("LoginC",UIBarButtonItemStyle.Done, delegate { 
      UIApplication.SharedApplication.KeyWindow.RootViewController = RootController.Instance.LoginController; 
     }),true); 

     //Set a button to go to tableController 
     UITableViewController tableViewController = new UITableViewController(){ Title = "TableViewController" }; 
     tableViewController.View.Frame = UIScreen.MainScreen.Bounds; 
     tableViewController.View.BackgroundColor = UIColor.White; 
     tableViewController.HidesBottomBarWhenPushed = true;//To make tabbar disappear 
     this.NavigationItem.SetRightBarButtonItem (new UIBarButtonItem ("TableView",UIBarButtonItemStyle.Done, delegate { 
      this.NavigationController.PushViewController(tableViewController,true); 
     }),true); 
    } 
} 

Sie können Ihre eigenen Viewcontroller in RootController hinzuzufügen. cs anstelle meiner Sample-Controller.

Wenn Sie immer noch ein paar Probleme haben, lassen Sie es hier, ich werde es später überprüfen.

Ich hoffe, es kann Ihnen helfen.