当我的子类 PageViewController 尝试加载/显示时,收到一个错误,说它无法加载包中的 nib:NSBundle。
(见附件截图)
我已尝试删除 View Controller 和相关代码,然后从头开始重新创建它们。
我还完全关闭了 iPhone 模拟器和 Xamarin 并重新启动它们,希望这是与以下问题相关的问题:Could not load NIB in bundle - inspiration needed但都无济于事。
谁能指出我在这方面可能出了什么问题?
注意:在 iOS 设计器中创建的 OnboardingHome 和 OnboardingPAge ViewControllers 以及为每个创建的子类。
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 ("age -> _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 ("age -> 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("age {0}", this.PageIndex + 1);
}
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
Console.WriteLine ("age -> 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 的属性中指定一个类名。
这将生成一个像这样的新 VC-Class:
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 设计器不使用它们。所以我怀疑这可能是您问题的根源,因为您的错误消息中也提到了字符串“vc_onboardPage”。
我会尝试执行以下操作:
欢迎评论。
关于c# - MonoTouch - 无法在 bundle : 'NSBundle' 中加载 NIB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31654202/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |