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

标签 c# ios xamarin.ios uipageviewcontroller

当我的子类 PageViewController 尝试加载/显示时,出现错误,指出无法加载 bundle 中的 Nib :NSBundle。

(请参阅随附的文件屏幕截图)

enter image description here

我尝试删除 View Controller 和相关代码,然后从头开始重新创建它们。

我还完全关闭了 iPhone 模拟器和 Xamarin,然后重新启动它们,希望这是与以下问题相关的问题:Could not load NIB in bundle - inspiration needed但一切都无济于事。

有人可以指出我可能哪里出了问题吗?

注意:OnboardingHome 和 OnboardingPAge ViewController 是在 iOS Designer 中创建的,并为每个 ViewController 创建子类。

VC_OnboardingHome.cs

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

        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 >= (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 <= 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't have a superview.
        base.DidReceiveMemoryWarning ();

        // Release any cached data, images, etc that aren't in use.
    }
}
}

vc_onboardPage.cs

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("vc_onboardPage", null)
    {
        Console.WriteLine ("Page -> _constructor");
        // this.PageIndex = this.;
    }

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

        // Release any cached data, images, etc that aren't in use.
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        lbl_HeaderTxt.Text = "Label is here";
        Console.WriteLine ("Page -> ViewDidLoad");

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

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

        Console.WriteLine ("Page -> ViewDidAppear");
    }
}
}

堆栈跟踪

2015-07-27 14:13:07.663 Performance[8146:1518846] Page -> _constructor
2015-07-27 14:13:11.291 Performance[8146:1518846] Unhandled managed exception:
Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: Could not load NIB in bundle: 'NSBundle </Users/johncogan/Library/Developer/CoreSimulator/Devices/116BF4AA-5D28-4B3A-91F2-B9FA4FBFDEE8/data/Containers/Bundle/Application/5BD82BA5-65EE-496F-8BCE-887FEC5522AD/Performance.app> (loaded)' with name 'vc_onboardPage' (Foundation.MonoTouchException)
    at ObjCRuntime.Runtime.ThrowNSException (IntPtr ns_exception) [0x00000] in /Users/builder/data/lanes/1962/8b265d64/source/maccore/src/ObjCRuntime/Runtime.cs:167 
    at ObjCRuntime.Runtime.throw_ns_exception (IntPtr exc) [0x00000] 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) [0x0005f] in /Users/builder/data/lanes/1962/8b265d64/source/maccore/src/build/ios/native/UIKit/UIPageViewController.g.cs:148 
    at Performance.VC_OnboardingHome.ViewDidLoad () [0x00055] 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) [0x00005] 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) [0x00038] in /Users/builder/data/lanes/1962/8b265d64/source/maccore/src/UIKit/UIApplication.cs:47 
    at Performance.Application.Main (System.String[] args) [0x00008] in /Users/johncogan/Development/xamarin/Performance_Shared/Performance/Main.cs:14 
2015-07-27 14:13:11.292 Performance[8146:1518846] critical: Stacktrace:

最佳答案

我假设您正在使用 Storyboard并在 Xamarin 的内置 iOS 设计器中对其进行编辑。

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

Specifying a Class Property to a VC

这将生成一个新的 VC 类,如下所示:

partial class vc_test : UIView
{
    public vc_test (IntPtr handle) : base (handle)
    {
    }
}

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

// public vc_onboardPage (IntPtr handle) : base (handle)
public vc_onboardPage () : base("vc_onboardPage", null)

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

我会尝试执行以下操作:

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

欢迎评论。

关于c# - MonoTouch - 无法加载 bundle : 'NSBundle' 中的 NIB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31654202/

相关文章:

c# - 在向导控件的 asp.net 中隐藏代码后面的下一个按钮

objective-c - UIPopoverController的奇怪错误

iOS9:在导航栏中隐藏 SegmentedControl 后,标题没有出现

c# - xamarin 意图扩展与 netstandard 导致 MT2001 错误

iphone - UIButton 的响应时间

c# - IronPython 2.7 可以调用异步 .NET 函数吗?

c# - Application.Current.MainPage 与 Navigation.PushAsync() 与 Navigation.PushModalAsync()

c# - 如何在 xamarin iOS 中使用 AVPlayerViewController (AVKit) 播放视频

c# - 隐式与显式转换运算符最佳实践

ios - mediaTypesRequiringUserActionForPlayback 无法工作 Swift