菜鸟教程小白 发表于 2022-12-12 15:54:48

c# - MonoTouch - 无法在 bundle : 'NSBundle' 中加载 NIB


                                            <p><p>当我的子类 PageViewController 尝试加载/显示时,收到一个错误,说它无法加载包中的 nib:NSBundle。</p>

<p>(见附件截图)</p>

<p> <a href="/image/megdx.png" rel="noreferrer noopener nofollow"><img src="/image/megdx.png" alt="enter image description here"/></a> </p>

<p>我已尝试删除 ViewController 和相关代码,然后从头开始重新创建它们。</p>

<p>我还完全关闭了 iPhone 模拟器和 Xamarin 并重新启动它们,希望这是与以下问题相关的问题:<a href="https://stackoverflow.com/questions/9414036/could-not-load-nib-in-bundle-inspiration-needed" rel="noreferrer noopener nofollow">Could not load NIB in bundle - inspiration needed</a>但都无济于事。</p>

<p>谁能指出我在这方面可能出了什么问题?</p>

<p>注意:在 iOS 设计器中创建的 OnboardingHome 和 OnboardingPAge ViewControllers 以及为每个创建的子类。</p>

<p>VC_OnboardingHome.cs</p>

<pre><code>using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;

namespace Performance
{
partial class VC_OnboardingHome : UIViewController
{
    private UIPageViewController pageViewController;
    private int pageCount = 3;

    public VC_OnboardingHome (IntPtr handle) : base (handle)
    {
    }

    public override void ViewDidLoad ()
    {
      base.ViewDidLoad ();
      vc_onboardPage firstPage = new vc_onboardPage();
      firstPage.PageIndex = 0;

      pageViewController = new UIPageViewController (
            UIPageViewControllerTransitionStyle.PageCurl,
            UIPageViewControllerNavigationOrientation.Horizontal,
            UIPageViewControllerSpineLocation.Min);

      this.pageViewController.SetViewControllers (
            new UIViewController[] { firstPage },
            UIPageViewControllerNavigationDirection.Forward,
            false,
            s =&gt; {
            }
      );

      this.pageViewController.GetNextViewController = this.GetNextViewController;
      //this.pageViewController.GetNextViewController(this.pageViewController, this);

      this.pageViewController.GetPreviousViewController = this.GetPreviousViewController;
      //this.pageViewController.GetNextViewController(this.pageViewController, this);

      this.pageViewController.View.Frame = this.View.Bounds;
      //this.View.AddSubview(this.pageViewController.View);
      this.View.AddSubview (pageViewController.View);
    }

    private UIViewController GetNextViewController(UIPageViewController pageController, UIViewController referenceViewController)
    {
      vc_onboardPage currentPageController = referenceViewController as vc_onboardPage;

      if (currentPageController.PageIndex &gt;= (this.pageCount - 1)){
            return null;
      }else{
            int nextPageIndex = currentPageController.PageIndex + 1;
            vc_onboardPage vc_onboardPage = new vc_onboardPage ();
            vc_onboardPage.PageIndex = nextPageIndex;
            return vc_onboardPage;
      }         
    }      

    private UIViewController GetPreviousViewController(UIPageViewController pageController, UIViewController referenceViewController)
    {
      vc_onboardPage currentPageController = referenceViewController as vc_onboardPage;

      if (currentPageController.PageIndex &lt;= 0){
            return null;
      }else{
            int previousPageIndex = currentPageController.PageIndex - 1;
            vc_onboardPage vc_onboardPage = new vc_onboardPage ();
            vc_onboardPage.PageIndex = previousPageIndex;
            return vc_onboardPage;
      }         
    }

    public override void DidReceiveMemoryWarning ()
    {
      // Releases the view if it doesn&#39;t have a superview.
      base.DidReceiveMemoryWarning ();

      // Release any cached data, images, etc that aren&#39;t in use.
    }
}
}
</code></pre>

<p>vc_onboardPage.cs</p>

<pre><code>using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;

namespace Performance
{
partial class vc_onboardPage : UIViewController
{
    private int pageIndex;

    public int PageIndex
    {
      get;
      set;
    }

    // public vc_onboardPage (IntPtr handle) : base (handle)
    public vc_onboardPage () : base(&#34;vc_onboardPage&#34;, null)
    {
      Console.WriteLine (&#34;Page -&gt; _constructor&#34;);
      // this.PageIndex = this.;
    }

    public override void DidReceiveMemoryWarning ()
    {
      // Releases the view if it doesn&#39;t have a superview.
      base.DidReceiveMemoryWarning ();

      // Release any cached data, images, etc that aren&#39;t in use.
    }

    public override void ViewDidLoad ()
    {
      base.ViewDidLoad ();
      lbl_HeaderTxt.Text = &#34;Label is here&#34;;
      Console.WriteLine (&#34;Page -&gt; ViewDidLoad&#34;);

      // Perform any additional setup after loading the view, typically from a nib.
      // this.imgView.Image = UIImage.FromFile(string.Format(&#34;images/{0}.jpg&#34;, this.PageIndex + 1));
      lbl_HeaderTxt.Text = string.Format(&#34;Page {0}&#34;, this.PageIndex + 1);
    }

    public override void ViewDidAppear (bool animated)
    {
      base.ViewDidAppear (animated);

      Console.WriteLine (&#34;Page -&gt; ViewDidAppear&#34;);
    }
}
}
</code></pre>

<p>堆栈跟踪</p>

<pre><code>2015-07-27 14:13:07.663 Performance Page -&gt; _constructor
2015-07-27 14:13:11.291 Performance Unhandled managed exception:
Objective-C exception thrown.Name: NSInternalInconsistencyException Reason: Could not load NIB in bundle: &#39;NSBundle &lt;/Users/johncogan/Library/Developer/CoreSimulator/Devices/116BF4AA-5D28-4B3A-91F2-B9FA4FBFDEE8/data/Containers/Bundle/Application/5BD82BA5-65EE-496F-8BCE-887FEC5522AD/Performance.app&gt; (loaded)&#39; with name &#39;vc_onboardPage&#39; (Foundation.MonoTouchException)
    at ObjCRuntime.Runtime.ThrowNSException (IntPtr ns_exception) in /Users/builder/data/lanes/1962/8b265d64/source/maccore/src/ObjCRuntime/Runtime.cs:167
    at ObjCRuntime.Runtime.throw_ns_exception (IntPtr exc) in /Users/builder/data/lanes/1962/8b265d64/source/maccore/runtime/Delegates.generated.cs:100
    at (wrapper native-to-managed) ObjCRuntime.Runtime:throw_ns_exception (intptr)
    at (wrapper managed-to-native) ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_int_bool_IntPtr (intptr,intptr,intptr,int,bool,intptr)
    at UIKit.UIPageViewController.SetViewControllers (UIKit.UIViewController[] viewControllers, UIPageViewControllerNavigationDirection direction, Boolean animated, UIKit.UICompletionHandler completionHandler) in /Users/builder/data/lanes/1962/8b265d64/source/maccore/src/build/ios/native/UIKit/UIPageViewController.g.cs:148
    at Performance.VC_OnboardingHome.ViewDidLoad () in /Users/johncogan/Development/xamarin/Performance_Shared/Performance/VC_OnboardingHome.cs:28
    at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
    at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) in /Users/builder/data/lanes/1962/8b265d64/source/maccore/src/UIKit/UIApplication.cs:63
    at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) in /Users/builder/data/lanes/1962/8b265d64/source/maccore/src/UIKit/UIApplication.cs:47
    at Performance.Application.Main (System.String[] args) in /Users/johncogan/Development/xamarin/Performance_Shared/Performance/Main.cs:14
2015-07-27 14:13:11.292 Performance critical: Stacktrace:
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我假设您正在使用 Storyboard 并在 Xamarin 的内置 iOS 设计器中对其进行编辑。</p>

<p>您是如何将 ViewController 添加到您的项目中的?根据我的经验,在 Xamarin 的 iOS 设计器中执行此操作的最安全的方法之一就是在 ViewController 的属性中指定一个类名。</p>

<p> <a href="/image/CJBRW.png" rel="noreferrer noopener nofollow"><img src="/image/CJBRW.png" alt="Specifying a Class Property to a VC"/></a> </p>

<p>这将生成一个像这样的新 VC-Class:</p>

<pre><code>partial class vc_test : UIView
{
    public vc_test (IntPtr handle) : base (handle)
    {
    }
}
</code></pre>

<p>请注意,构造函数使用 IntPtr 作为参数调用基本构造函数。将其与您的构造函数进行对比,该构造函数使用字符串参数调用它:</p>

<pre><code>// public vc_onboardPage (IntPtr handle) : base (handle)
public vc_onboardPage () : base(&#34;vc_onboardPage&#34;, null)
</code></pre>

<p>当我上次检查时,字符串参数仅在使用 NIB/XIB 时有效,iOS 设计器不使用它们。所以我怀疑这可能是您问题的根源,因为您的错误消息中也提到了字符串“vc_onboardPage”。</p>

<p>我会尝试执行以下操作:</p>

<ul>
<li>在 iOS 设计器中将 View 的“类”属性重命名为全新的名称。</li>
<li>清理并重建您的解决方案</li>
</ul>

<p>欢迎评论。</p></p>
                                   
                                                <p style="font-size: 20px;">关于c# - MonoTouch - 无法在 bundle:&#39;NSBundle&#39;中加载 NIB,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/31654202/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/31654202/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: c# - MonoTouch - 无法在 bundle : &#39;NSBundle&#39; 中加载 NIB