UIPageViewController第二个视图变为空白

时间:2013-04-15 18:47:09

标签: ios xamarin.ios uipageviewcontroller

在我的应用中使用UIPageViewController时,我遇到了一些非常奇怪的行为。在第一次滑动到下一个视图时或者任何时候滑动方向改变(即在列表的末尾并返回),结果视图为空白,我需要再次滑动相同的方向以显示相应的视图。

包含3个视图的示例工作流程:

  1. 第一个视图
  2. 向右滑动
  3. 第二个视图闪烁进出
  4. 向右滑动
  5. 提出第二个视图
  6. 向右滑动
  7. 提出第三个观点
  8. 向左滑动
  9. 第二个视图闪烁进出
  10. 向左滑动
  11. 提出第二个视图
  12. 我注意到当眨眼发生时通过记录(上面的2&3),事件的顺序如下:

    1. 调用GetNextViewController
    2. 调用GetPreviousViewController
    3. 调用GetNextViewController
    4. 然后当我再次滑动时,适当调用GetNextViewController并显示视图。

      显然,我的问题是第二个视图应该显示没有闪烁进出。我尝试了各种各样的东西,但没有任何成果。

      更新

      以下是一个重现问题的示例单控制器应用程序:

      using System;
      using System.Drawing;
      
      using MonoTouch.Foundation;
      using MonoTouch.UIKit;
      using System.Collections.Generic;
      
      namespace TestPageView
      {
          public partial class TestPageViewViewController : UIViewController
          {
              public TestPageViewViewController () : base ("TestPageViewViewController", null)
              {
              }
      
              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 ();
      
                  var controllers = new List<UIViewController>
                  {
                      new TestViewController(View, 1),
                      new TestViewController(View, 2),
                      new TestViewController(View, 3),
                      new TestViewController(View, 4)
                  };
      
                  var _pageViewController = new UIPageViewController(UIPageViewControllerTransitionStyle.Scroll, UIPageViewControllerNavigationOrientation.Horizontal);
                  var dataSource = new ViewDataSource(controllers);
                  _pageViewController.DataSource = dataSource;
                  //ViewDataSource has an indexer that returns it's controllers
                  _pageViewController.SetViewControllers(new[] { controllers[0] }, UIPageViewControllerNavigationDirection.Forward, false, null);
                  View.AddSubview(_pageViewController.View);
              }
      
              public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
              {
                  // Return true for supported orientations
                  return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
              }
          }
      
          public class TestViewController : UIViewController
          {
              private readonly UIView _parent;
              private readonly int _number;
              public TestViewController(UIView parent, int number)
              {
                  _parent = parent;
                  _number = number;
              }
      
              public override void ViewDidLoad ()
              {
                  base.ViewDidLoad ();
      
                  var textView = new UITextView(_parent.Bounds);
                  textView.TextAlignment = UITextAlignment.Center;
                  textView.AttributedText = new NSAttributedString(_number.ToString(), UIFont.BoldSystemFontOfSize(18));
                  View.Add(textView);
              }
          }
      
          public class ViewDataSource : UIPageViewControllerDataSource
          {
              private LinkedListNode<UIViewController> current;
      
              public ViewDataSource(IEnumerable<UIViewController> controllers)
              {
                  var controllerLiist = new LinkedList<UIViewController>(controllers);
                  current = controllerLiist.First;
              }
      
              public override UIViewController GetPreviousViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
              {
                  if(current.Previous == null){
                      Console.WriteLine("returning previous nothing");
                      return null;
                  }
                  Console.WriteLine("returning previous something");
                  current = current.Previous;
                  return current.Value;
              }
      
              public override UIViewController GetNextViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
              {
                  if(current.Next == null){
                      Console.WriteLine("returning next nothing");
                      return null;
                  }
                  Console.WriteLine("returning next something");
                  current = current.Next;
                  return current.Value;
              }
          }
      }
      

1 个答案:

答案 0 :(得分:9)

iOS将调用GetPreviousViewController和GetNextViewController两次:首先是当前控制器,第二次是当前控制器的后空翻。

你应该这样做

  

current = current.Previous;

仅在第一次通话时。

检查referenceViewController以执行此操作:

public override UIViewController GetPreviousViewController (UIPageViewController pageViewController, UIViewController referenceViewController)
    {
        var __c = Pages.Find(referenceViewController);

        if (__c.Previous != null)
            return __c.Previous.Value;
        else
            return null;
    }
    public override UIViewController GetNextViewController (UIPageViewController pageViewController, UIViewController referenceViewController)
    {
        var __c = Pages.Find(referenceViewController);

        if (__c.Next != null)
            return __c.Next.Value;
        else
            return null;
    }