ios – UIPageViewController第二个视图变为空白

前端之家收集整理的这篇文章主要介绍了ios – UIPageViewController第二个视图变为空白前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中使用UIPageViewController时,我遇到了一些非常奇怪的行为.在第一次滑动到下一个视图或任何时候滑动方向改变(即在列表的末尾并返回)时,结果视图为空白,我需要再次滑动相同的方向以显示相应的视图.

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

>首先提出的观点
>向右滑动
>第二个视图闪烁
>向右滑动
>提出第二种观点
>向右滑动
>提出第三种观点
>向左滑动
>第二个视图闪烁
>向左滑动
>提出第二种观点

当发生眨眼时(上面的2&3),我注意到通过记录,事件的顺序如下:

>调用GetNextViewController
>调用GetPrevIoUsViewController
>调用GetNextViewController

然后当我再次滑动时,适当调用GetNextViewController并显示视图.

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

UPDATE

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

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),3),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;
        }
    }
}

解决方法

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;
    }

猜你在找的iOS相关文章