App crashed when add a sub view which load from .xib file to main view

HelloTaxiHelloTaxi Member ✭✭

I create a custom view named HelloView with HelloView.xib. In ViewController.ViewDidLoad(), I load the HelloView from the HelloView.xib file, and call AddSubview method to add this view into ViewController's root view as its subview. Sometimes, the app can run normally, but most of the time, it crashed, the attachement is the crash report.

Operating System Mac OS X 10.14.5
Visual Studio Community 2019 for Mac Version 8.1 (build 2742)
Mono Framework MDK Runtime: Mono 5.18.1.28 (2018-08/223ea7ef92e) (64-bit), Package version: 518010028
Xamarin.Mac Version: 5.10.0.153 (Visual Studio Community)

Here's my code:

HelloView.cs

namespace TestMac
{
    public partial class HelloView : AppKit.NSView
    {
        #region Constructors

        // Called when created from unmanaged code
        public HelloView(IntPtr handle) : base(handle)
        {
            Initialize();
        }

        // Called when created directly from a XIB file
        [Export("initWithCoder:")]
        public HelloView(NSCoder coder) : base(coder)
        {
            Initialize();
        }

        // Shared initialization code
        void Initialize()
        {
        }

        #endregion

        public static HelloView Create()
        {
            NSArray arr;
            NSBundle.MainBundle.LoadNibNamed(nameof(HelloView), null, out arr);
            return arr.GetItem<HelloView>(0);
        }
    }
}

HelloView.xib
Just add a button on the view, no outlets, no actions.

ViewController.cs

namespace TestMac
{
    public partial class ViewController : NSViewController
    {
        public ViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            var viewHello = HelloView.Create();
            this.View.AddSubview(viewHello);
        }
    }
}

THX.

Best Answer

  • HelloTaxiHelloTaxi ✭✭
    Accepted Answer

    Thank you @ChrisHamons , I've found the cause after I test the similar project in Swift.
    The problematic code is arr.GetItem<HelloView>(0).
    According to my test, the HelloView is not always at the index of 0, so I changed the code of HelloView.Create method:

            public static HelloView Create()
            {
                NSArray arr;
                NSBundle.MainBundle.LoadNibNamed(nameof(HelloView), null, out arr);
                for(nuint i = 0; i < arr.Count; i++)
                {
                    var v = arr.GetItem<NSObject>(i);
                    if (v is HelloView) return v as HelloView;
                }
                return null;
            }
    

    Now, it works fine.

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    I don't see an obvious issue at first glance, please consider posting an issue at https://github.com/xamarin/xamarin-macios/issues/new and attach the project in question for us to look at.

  • HelloTaxiHelloTaxi Member ✭✭
    Accepted Answer

    Thank you @ChrisHamons , I've found the cause after I test the similar project in Swift.
    The problematic code is arr.GetItem<HelloView>(0).
    According to my test, the HelloView is not always at the index of 0, so I changed the code of HelloView.Create method:

            public static HelloView Create()
            {
                NSArray arr;
                NSBundle.MainBundle.LoadNibNamed(nameof(HelloView), null, out arr);
                for(nuint i = 0; i < arr.Count; i++)
                {
                    var v = arr.GetItem<NSObject>(i);
                    if (v is HelloView) return v as HelloView;
                }
                return null;
            }
    

    Now, it works fine.

Sign In or Register to comment.