Xamarin iOS UINavigationController NavigationBar被处置掉了

时间:2018-02-22 15:16:24

标签: ios xamarin.ios uinavigationcontroller uitabbarcontroller

我有一个带标签栏的应用程序,每个标签位于单独的UINavigationController中。

架构:

  • HomeScreen:UITabBarController
    • Page1:ViewController
    • 第2页:ViewController
    • ...

这些页面位于UINavigationControllers中。 HomeScreen在AppDelegate中设置为RootViewController。

问题是每个页面中的NavigationBar会在几秒钟后由GC处理。 每当我切换标签时,我都会得到一个SIGSEGV:

2018-02-22 15:56:07.392 MyApp.IOS[99736:4863977] critical: Stacktrace:
2018-02-22 15:56:07.392 MyApp.IOS[99736:4863977] critical:   at <unknown> <0xffffffff>
2018-02-22 15:56:07.393 MyApp.IOS[99736:4863977] critical:   at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) [0x0005c] in <e0596b82250c450abdf075bc558add28>:0
2018-02-22 15:56:07.393 MyApp.IOS[99736:4863977] critical:   at UIKit.UIApplication.Main (string[],intptr,intptr) [0x00005] in /Users/builder/data/lanes/5665/db807ec9/source/xamarin-macios/src/UIKit/UIApplication.cs:79
2018-02-22 15:56:07.393 MyApp.IOS[99736:4863977] critical:   at UIKit.UIApplication.Main (string[],string,string) [0x00038] in /Users/builder/data/lanes/5665/db807ec9/source/xamarin-macios/src/UIKit/UIApplication.cs:63
2018-02-22 15:56:07.394 MyApp.IOS[99736:4863977] critical:   at MyApp.IOS.Application.Main (string[]) [0x00002] in /Users/me/Projects/com/App/MyApp.IOS/Main.cs:15
2018-02-22 15:56:07.394 MyApp.IOS[99736:4863977] critical:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) [0x00051] in <9e2aa0160a6445caa466dba48035aadf>:0
2018-02-22 15:56:07.394 MyApp.IOS[99736:4863977] critical: 
Native stacktrace:

2018-02-22 15:56:07.401 MyApp.IOS[99736:4863977] critical:  0   MyApp.IOS                  0x000000010e8f9874 mono_handle_native_crash + 244
2018-02-22 15:56:07.401 MyApp.IOS[99736:4863977] critical:  1   MyApp.IOS                  0x000000010e907980 mono_sigsegv_signal_handler + 288
2018-02-22 15:56:07.401 MyApp.IOS[99736:4863977] critical:  2   libsystem_platform.dylib            0x000000011d957f5a _sigtramp + 26
2018-02-22 15:56:07.401 MyApp.IOS[99736:4863977] critical:  3   ???                                 0x00007ffee147ccc8 0x0 + 140732677999816
2018-02-22 15:56:07.401 MyApp.IOS[99736:4863977] critical:  4   UIKit                               0x00000001133d0588 -[UIViewController _preferredWhitePointAdaptivityStyle] + 37
2018-02-22 15:56:07.401 MyApp.IOS[99736:4863977] critical:  5   UIKit                               0x00000001132073d6 -[UIApplication _updateCurrentWhitePointAdaptivityStyle] + 62
2018-02-22 15:56:07.402 MyApp.IOS[99736:4863977] critical:  6   UIKit                               0x00000001133d0fbb -[UIViewController setNeedsWhitePointAdaptivityStyleUpdate] + 100
2018-02-22 15:56:07.402 MyApp.IOS[99736:4863977] critical:  7   UIKit                               0x000000011341d3a0 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 3768
2018-02-22 15:56:07.402 MyApp.IOS[99736:4863977] critical:  8   UIKit                               0x000000011341ba3d -[UITabBarController transitionFromViewController:toViewController:] + 59
2018-02-22 15:56:07.402 MyApp.IOS[99736:4863977] critical:  9   UIKit                               0x00000001134179f0 -[UITabBarController _setSelectedViewController:] + 410
2018-02-22 15:56:07.402 MyApp.IOS[99736:4863977] critical:  10  UIKit                               0x00000001134177bf -[UITabBarController setSelectedViewController:] + 109
2018-02-22 15:56:07.402 MyApp.IOS[99736:4863977] critical:  11  UIKit                               0x000000011341b96a -[UITabBarController _tabBarItemClicked:] + 599
2018-02-22 15:56:07.402 MyApp.IOS[99736:4863977] critical:  12  UIKit                               0x0000000113206972 -[UIApplication sendAction:to:from:forEvent:] + 83
2018-02-22 15:56:07.403 MyApp.IOS[99736:4863977] critical:  13  UIKit                               0x00000001136a8c99 -[UITabBar _sendAction:withEvent:] + 597
2018-02-22 15:56:07.403 MyApp.IOS[99736:4863977] critical:  14  UIKit                               0x0000000113206972 -[UIApplication sendAction:to:from:forEvent:] + 83
2018-02-22 15:56:07.403 MyApp.IOS[99736:4863977] critical:  15  UIKit                               0x0000000113385c3c -[UIControl sendAction:to:forEvent:] + 67
2018-02-22 15:56:07.403 MyApp.IOS[99736:4863977] critical:  16  UIKit                               0x0000000113385f59 -[UIControl _sendActionsForEvents:withEvent:] + 450
2018-02-22 15:56:07.403 MyApp.IOS[99736:4863977] critical:  17  UIKit                               0x00000001136ab69f -[UITabBar _buttonUp:] + 113
2018-02-22 15:56:07.403 MyApp.IOS[99736:4863977] critical:  18  UIKit                               0x0000000113206972 -[UIApplication sendAction:to:from:forEvent:] + 83
2018-02-22 15:56:07.404 MyApp.IOS[99736:4863977] critical:  19  UIKit                               0x0000000113385c3c -[UIControl sendAction:to:forEvent:] + 67
2018-02-22 15:56:07.404 MyApp.IOS[99736:4863977] critical:  20  UIKit                               0x0000000113385f59 -[UIControl _sendActionsForEvents:withEvent:] + 450
2018-02-22 15:56:07.404 MyApp.IOS[99736:4863977] critical:  21  UIKit                               0x0000000113384e86 -[UIControl touchesEnded:withEvent:] + 618
2018-02-22 15:56:07.404 MyApp.IOS[99736:4863977] critical:  22  UIKit                               0x000000011327c807 -[UIWindow _sendTouchesForEvent:] + 2807
2018-02-22 15:56:07.404 MyApp.IOS[99736:4863977] critical:  23  UIKit                               0x000000011327df2a -[UIWindow sendEvent:] + 4124
2018-02-22 15:56:07.404 MyApp.IOS[99736:4863977] critical:  24  UIKit                               0x0000000113221365 -[UIApplication sendEvent:] + 352
2018-02-22 15:56:07.405 MyApp.IOS[99736:4863977] critical:  25  UIKit                               0x0000000113b6da1d __dispatchPreprocessedEventFromEventQueue + 2809
2018-02-22 15:56:07.405 MyApp.IOS[99736:4863977] critical:  26  UIKit                               0x0000000113b70672 __handleEventQueueInternal + 5957
2018-02-22 15:56:07.405 MyApp.IOS[99736:4863977] critical:  27  CoreFoundation                      0x000000010f154101 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
2018-02-22 15:56:07.405 MyApp.IOS[99736:4863977] critical:  28  CoreFoundation                      0x000000010f1f3f71 __CFRunLoopDoSource0 + 81
2018-02-22 15:56:07.405 MyApp.IOS[99736:4863977] critical:  29  CoreFoundation                      0x000000010f138a19 __CFRunLoopDoSources0 + 185
2018-02-22 15:56:07.406 MyApp.IOS[99736:4863977] critical:  30  CoreFoundation                      0x000000010f137fff __CFRunLoopRun + 1279
2018-02-22 15:56:07.406 MyApp.IOS[99736:4863977] critical:  31  CoreFoundation                      0x000000010f137889 CFRunLoopRunSpecific + 409
2018-02-22 15:56:07.406 MyApp.IOS[99736:4863977] critical:  32  GraphicsServices                    0x00000001202ce9c6 GSEventRunModal + 62
2018-02-22 15:56:07.406 MyApp.IOS[99736:4863977] critical:  33  UIKit                               0x00000001132055d6 UIApplicationMain + 159
2018-02-22 15:56:07.406 MyApp.IOS[99736:4863977] critical:  34  ???                                 0x000000013c8eb708 0x0 + 5310953224
2018-02-22 15:56:07.406 MyApp.IOS[99736:4863977] critical:  35  ???                                 0x000000013c8eb343 0x0 + 5310952259
2018-02-22 15:56:07.407 MyApp.IOS[99736:4863977] critical: 
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

代码:

的AppDelegate:

HomeScreen也是AppDelegate中的变量

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) {
UITabBar.Appearance.SelectedImageTintColor = Colors.BackgroundColorLightGreen;

UITabBar.Appearance.TintColor = Colors.BackgroundColorLightGreen;
homeScreen = new HomeScreen();
Window = new UIWindow(UIScreen.MainScreen.Bounds);
Window.RootViewController = homeScreen;
Window.MakeKeyAndVisible();
return true;
}

主屏幕:

public class HomeScreen : MyTabBarController
{
    protected List<UINavigationController> controllers;

    public HomeScreen() : base()
    {
        Initialize();
    }

    void Initialize()
    {
        Title = "Title";

        ColorToolBar = Colors.BackgroundColorLightGreen;
        ColorTabBar = Colors.BackgroundColor;

        controllers = new List<UINavigationController>();

        var page1 = new UIViewController();
        var page1nav = new UINavigationController(page1);
        page1nav.TabBarItem = new UITabBarItem();
        page1nav.TabBarItem.Title = "xxx";
        page1nav.TabBarItem.Image = Shared.ResourceLoader.LoadImage("xxx", 30);
        controllers.Add(page1nav);

        var page2 = new UIViewController();
        var page2nav = new UINavigationController(page2);
        page2nav.TabBarItem = new UITabBarItem();
        page2nav.TabBarItem.Title = "xxx";
        page2nav.TabBarItem.Image = Shared.ResourceLoader.LoadImage("xxx", 30);
        controllers.Add(page2nav);

        ViewControllers = controllers.ToArray();
    }
}

在页面本身我设置了标题,以及NavigationItem上的一些按钮。

我无法弄清楚我做错了什么

编辑:

我刚在AppDelegate中使用此代码尝试了相同的架构,我得到了完全相同的错误,我认为这是最新的Xamarin中的错误?

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {

        Window = new UIWindow(UIScreen.MainScreen.Bounds);

        var viewController1 = new UIViewController()
        {
            Title = "First View Controller",
        };

        var viewController2 = new UIViewController()
        {
            Title = "Second View Controller",
        };

        // Initialize the nav controllers, setting their Root controller to the above controllers
        tab1NavController = new UINavigationController(viewController1);
        tab2NavController = new UINavigationController(viewController2);


        homeScreen = new UITabBarController();

        homeScreen.ViewControllers = new UIViewController[] {
            tab1NavController,
            tab2NavController,
        };

        Window.RootViewController = homeScreen;
        Window.MakeKeyAndVisible();
        return true;
    }

0 个答案:

没有答案