【问题标题】:Difficulty with TabBarControllers intermixed with NavigationControllersTabBarControllers 与 NavigationControllers 混合的困难
【发布时间】:2016-08-24 15:39:33
【问题描述】:

我有一个 Xamarin.iOS 应用,其中具有以下结构:

NavController(根:登录)

--> TabBarController (Home) - (Search) - (Profile)

-----> 导航控制器(根:主页)

---------->表格控制器

----------------->细节控制器

----->NavController(根:搜索)

...等

我目前在使用导航项时遇到困难,特别是后退按钮项。

我不想通过返回按钮返回登录页面,所以在我的HomeController 中,我通过说隐藏了back button

TabBarController.NavigationItem.HidesBackButton = true;

当我进入下一个屏幕 (TableController) 时,我希望看到返回到 HomeController 的后退按钮,但是我目前的方法有一个返回到 Login 控制器的按钮

this.TabBarController.NavigationItem.HidesBackButton = false;

感谢您的帮助

【问题讨论】:

  • 您是否使用 push segue 将登录视图控制器与标签栏控制器连接起来?也许您可以尝试使用其他 segue,这样它们就不会共享一个导航控制器实例。

标签: ios uinavigationcontroller uitabbarcontroller uinavigationitem


【解决方案1】:

为了你想要的效果,我建议你创建一个对象(我以“RootController”为例)来管理应用程序的Window.RootController。

不知道大家有没有修改window的RootController的经验,就按照我的步骤来吧:

首先创建一个新项目,删除storyboard和Viewcontroller.cs。(不要忘记删除info.plist中storyboard的bundle)

然后重写你的 AppDelegate.cs,像这样:

[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.
    }
}

这是 RootController.cs,其中包含一个 UINavigationController 和一个 UITabBarController(Home - Search - Profile):

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;
    }
}

这是 HomeController.cs,它可以根据需要推送 UITableViewController,如果需要,还有返回 LoginController 的按钮:

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);
    }
}

您可以在 RootController.cs 中添加自己的 ViewController,而不是我的示例控制器。

如果还有问题,就留在这里,我稍后再检查。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多