【问题标题】:PageViewController on Xamarin will not load child contentXamarin 上的 PageViewController 不会加载子内容
【发布时间】:2016-08-08 18:22:36
【问题描述】:

我对移动开发很陌生,如果其中一些看起来微不足道,请原谅我。但是我试图让一个基本的 PageViewController 在 Xamarin iOS 中工作,并使用我拥有的许多不同的注册控制器,用于应用程序注册的不同阶段。所以用户将填写第一页,点击继续,然后进入第二步,依此类推。

我使用了一个来自 GitHub 的示例 PageViewController 项目,并且在我的模拟器上运行良好。但是,当我尝试将它与我的注册控制器一起使用时,出现错误

Foundation.MonoTouchException:抛出 Objective-C 异常。姓名: NSUnknownKeyException 原因:[ setValue:forUndefinedKey:]:这个类不是键值 键 DateOfBirthText 的编码兼容。

这是父视图控制器的 OnLoad 方法

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

            _pageViewController = Storyboard.InstantiateViewController("SignInPageViewController") as UIPageViewController;
            _pageViewController.DataSource = new PageViewControllerDataSource(this, _userInput);

            var startVc = ViewControllerAtIndex(0);
            var viewControllers = new[] {startVc};

            _pageViewController.SetViewControllers(viewControllers, UIPageViewControllerNavigationDirection.Forward, false, null);
            _pageViewController.View.Frame = new CGRect(0, 0, View.Frame.Width, View.Frame.Size.Height - 50);
            AddChildViewController(_pageViewController);
            View.AddSubview(_pageViewController.View);
            _pageViewController.DidMoveToParentViewController(this);            
        }

在线抛出错误

_pageViewController.SetViewControllers

这是试图加载到 PageViewController 并抛出错误的子控制器的设计器代码

{
    [Register ("SigninViewControllerStageOne")]
    partial class SigninViewControllerStageOne
    {
        [Outlet]
        [GeneratedCode ("iOS Designer", "1.0")]
        UITextField DateOfBirthText { get; set; }

        [Outlet]
        [GeneratedCode ("iOS Designer", "1.0")]
        UITextField NameText { get; set; }

        [Outlet]
        [GeneratedCode ("iOS Designer", "1.0")]
        UIButton NextStepButton { get; set; }

        [Outlet]
        [GeneratedCode ("iOS Designer", "1.0")]
        UITextField PhoneNumberText { get; set; }

        [Outlet]
        [GeneratedCode ("iOS Designer", "1.0")]
        UILabel TitleLabel { get; set; }

        void ReleaseDesignerOutlets ()
        {
            if (DateOfBirthText != null) {
                DateOfBirthText.Dispose ();
                DateOfBirthText = null;
            }
            if (NameText != null) {
                NameText.Dispose ();
                NameText = null;
            }
            if (NextStepButton != null) {
                NextStepButton.Dispose ();
                NextStepButton = null;
            }
            if (PhoneNumberText != null) {
                PhoneNumberText.Dispose ();
                PhoneNumberText = null;
            }
            if (TitleLabel != null) {
                TitleLabel.Dispose ();
                TitleLabel = null;
            }
        }
    }
}

更新:完整堆栈跟踪

未处理的异常:

Foundation.MonoTouchException:抛出 Objective-C 异常。姓名: NSUnknownKeyException 原因:[ setValue:forUndefinedKey:]:这个类不是键值 键 DateOfBirthText 的编码兼容。

本机堆栈跟踪:

0 核心基础 0x00958494 __exceptionPreprocess + 180

1 libobjc.A.dylib 0x08fd3e02 objc_exception_throw + 50

2 核心基础 0x009580b1 -[NSException 加注] + 17

3 基础 0x014777f8 -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 282

4 基础 0x013d1e6a _NSSetUsingKeyValueSetter + 115

5 基础 0x013d1def -[NSObject(NSKeyValueCoding) setValue:forKey:] + 295

6 UIKit 0x0397b931 -[UIViewController setValue:forKey:] + 85

7 基础 0x0140654b -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 384

8 UIKit 0x03bf8a62 -[UIRuntimeOutletConnection 连接] + 132

9 libobjc.A.dylib 0x08fe800c -[NSObject 执行选择器:] + 62

10 核心基础 0x00881131 -[NSArray makeObjectsPerformSelector:] + 273

11 UIKit 0x03bf70fc -[UINib instantiateWithOwner:options:] + 2102

12 UIKit 0x03983380 -[UIViewController _loadViewFromNibNamed:bundle:] + 429

13 UIKit 0x03983db8 -[UIViewController 加载视图] + 189

14 UIKit 0x039841c4 -[UIViewController loadViewIfRequired] + 154

15 UIKit 0x03984ab1 -[UIViewController 视图] + 35

16 UIKit 0x04038649 -[UIPageViewController _setViewControllers:withCurlOfType:fromLocation:direction:animated:notifyDelegate:completion:] + 2250

17 UIKit 0x0403a76a -[UIPageViewController setViewControllers:direction:animated:completion:] + 309

18 ??? 0x1a6a07cc 0x0 + 443156428

19 ??? 0x1a69f6d0 0x0 + 443152080

20 ??? 0x18d3200f 0x0 + 416489487

21 ??? 0x18d29c1d 0x0 + 416455709

22 BarPandaApp 0x001be859 mono_jit_runtime_invoke + 921

23 BarPandaApp 0x0027d0ce mono_runtime_invoke + 142

24 BarPandaApp 0x0035e225 xamarin_trampoline + 5605

25 UIKit 0x0397fb72 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 44

26 UIKit 0x03984692 -[UIViewController loadViewIfRequired] + 1384

27 UIKit 0x039d02bb -[UINavigationController _layoutViewController:] + 52

28 UIKit 0x039d0b75 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 421

29 UIKit 0x039d0d1c -[UINavigationController _startTransition:fromViewController:toViewController:] + 145

30 UIKit 0x039d2118 -[UINavigationController _startDeferredTransitionIfNeeded:] + 1038

31 UIKit 0x039d339e -[UINavigationController __viewWillLayoutSubviews] + 68

32 UIKit 0x03bb84b7 -[UILayoutContainerView layoutSubviews] + 252

33 UIKit 0x038843d4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 810

34 libobjc.A.dylib 0x08fe8059 -[NSObject performSelector:withObject:] + 70

35 石英核心 0x0b607096 -[CALayer 布局子层] + 144

36 石英核心 0x0b5fa8b6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388

37 石英核心 0x0b5fa71a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26

38 石英核心 0x0b5ecee7 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317

39 石英核心 0x0b621847 _ZN2CA11Transaction6commitEv + 561

40 石英核心 0x0b622108 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92

41 核心基础 0x0086a75e CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 30

42 核心基础 0x0086a6be __CFRunLoopDoObservers + 398

43 核心基础 0x0085f868 CFRunLoopRunSpecific + 504

44 CoreFoundation 0x0085f65b CFRunLoopRunInMode + 123

45 UIKit 0x037ae2da -[UIApplication _run] + 540

46 UIKit 0x037b3eb9 UIApplicationMain + 160

47 ??? 0x18d27144 0x0 + 416444740

48 ??? 0x18d26ef4 0x0 + 416444148

49 ??? 0x18d249a0 0x0 + 416434592

50 ??? 0x18d24620 0x0 + 416433696

51 ??? 0x18d24823 0x0 + 416434211

52 BarPandaApp 0x001be859 mono_jit_runtime_invoke + 921

53 BarPandaApp 0x0027d0ce mono_runtime_invoke + 142

54 BarPandaApp 0x002822d1 mono_runtime_exec_main + 401

55 BarPandaApp 0x00282095 mono_runtime_run_main + 645

56 BarPandaApp 0x0013a3d5 mono_jit_exec + 213

57 BarPandaApp 0x0036d73a xamarin_main + 第2474章

58 BarPandaApp 0x0036e351 主 + 65

59 libdyld.dylib 0x094ada25 开始 + 1

【问题讨论】:

  • 什么是完整的错误消息(在您的问题中的剪辑消息之后应该有一些 at 行,即:at (wrapper managed-to-native) .... at ..... at .....
  • 刚刚添加了完整的堆栈跟踪
  • 有趣的是,当我删除我为每个控件(在 VS IDE 设计器属性窗口中)指定的名称并将它们保留为默认控件,没有短名称来引用它们时,它可以工作

标签: ios xamarin xamarin.ios


【解决方案1】:

其实我觉得你不应该使用 UIPageViewController 来实现注册进度。

但是如果你只是需要它,有一个我为其他人写的示例,你可以查看这个链接来获取它:

xamarin.ios implement PageController with images only code

我建议你使用UINavigationController,它可以满足你的所有需求,有一个例子:

这是 AppDelegate 的代码(重写方法“FinishedLaunching”):

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
        SignUpStepViewController step1 = new SignUpStepViewController (1, UIColor.Red);
        SignUpStepViewController step2 = new SignUpStepViewController (2, UIColor.Green);
        SignUpStepViewController step3 = new SignUpStepViewController (3, UIColor.Blue);
        step1.SetNextController(step2);
        step2.SetNextController(step3);

        UINavigationController myNavigationController = new UINavigationController (step1);
        this.Window = new UIWindow (UIScreen.MainScreen.Bounds);
        this.Window.RootViewController = myNavigationController;
        this.Window.MakeKeyAndVisible ();
        return true;
    }

这是 SignUpStepViewController 的代码:

public class SignUpStepViewController : UIViewController
{
    public SignUpStepViewController (int index,UIColor color)
    {
        this.Title = "Step " + index;
        this.View.BackgroundColor = color;
    }

    public void SetNextController(SignUpStepViewController nextC)
    {
        UIBarButtonItem btnContinue = new UIBarButtonItem ("Continue",UIBarButtonItemStyle.Done, delegate {
            this.NavigationController.PushViewController(nextC,true);
        });
        this.NavigationItem.SetRightBarButtonItem (btnContinue,true);
    }
}

希望对你有帮助。

如果您还需要帮助,请在此处留言,我会稍后查看。

【讨论】:

  • 嗨,非常感谢代码 sn-p。今晚我将尝试使用导航控制器,看看效果如何。我仍在努力了解在不同情况下使用的最佳控制器和框架,所以这很有帮助。顺便说一句,您是否碰巧有一个很好的教程的链接,该教程解释了 IOS 中所有不同的导航控制器类型以及何时使用每种类型?
  • 我试过这段代码,但在 UIViewController 内部,this.NavigationController 为空
  • 不过,我在 VS 图形设计编辑器中创建了我的视图控制器。我已经创建了它的一个实例并将其传递给导航控制器,就像您在上面所做的那样,但是它只是加载一个黑屏,它不会加载我在 GUI 编辑器中在该控制器中创建的任何控件或内容。是不是少了一个步骤?
  • 我没有VS图形设计编辑器的经验,一直用OS X开发。但可以肯定的是,您需要将 NavigationController 作为应用程序窗口的 RootController。关于黑屏,尝试为你推送的ViewController的视图设置一个Frame,比如“this.View.Frame = UIScreen.MainScreen.Bounds”。如果您仍然无法解决它,您能否发布一些代码或屏幕截图让我更清楚地了解该问题。关于教程,我推荐苹果的文档。
猜你喜欢
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-08
相关资源
最近更新 更多