NSInvalidArgumentException:尝试从对象[0]插入nil

时间:2018-02-19 15:47:59

标签: ios .net f# xamarin.ios

我的UIPageViewControllerDataSource初始化如下:

[<Register ("FlatImagesPageViewDataSource")>]
type FlatImagesPageViewDataSource() as x = 
    inherit UIPageViewControllerDataSource()

let mutable isinitialised = false

let mutable flatImages : List<UIImage> = List.Empty

let mutable parentView : UIPageViewController = null

let mutable controllers : List<UIViewController> = List.Empty

let viewControllerAtIndex(index : int) = 
    let mutable result : UIViewController = null
    if flatImages.Length = 0 || index >= flatImages.Length then
        result <- null
    else 
        let controller = new FlatImagesContentViewController(new IntPtr())
        controller.GetImage <- flatImages.[index]
        controller.GetPageIndex <- index 
        if controllers.Length = 0 then
            ((controller :> UIViewController )::controllers) |> ignore
            int(0) |> ignore
        else 
            let listc  = List.toArray controllers
            listc.[0] <- (controller :> UIViewController)
            controllers <- Array.toList listc
            int(0) |> ignore
        result <- controller
    result


member this.GetParentView 
    with get() = parentView 
    and set(value) = (
        parentView <- value
        controllers <- Array.toList parentView.ViewControllers
    )

member this.GetFlatImages 
    with get() = flatImages 
    and set(value) = (
        flatImages <- value
        if not isinitialised then 
            parentView.View.UserInteractionEnabled <- true
            if flatImages.Length > 0 then 
                let initialcontroller = viewControllerAtIndex(0)
                let mutable viewControllers : UIViewController list = []
                viewControllers <- initialcontroller::viewControllers
                parentView.SetViewControllers(viewControllers |> List.toArray,UIPageViewControllerNavigationDirection.Forward,true,null)
                controllers <- viewControllers
            isinitialised <- true
        else
            ((controllers.[0]) :?> FlatImagesContentViewController).GetImage <- flatImages.[((parentView.ViewControllers.[0]) :?> FlatImagesContentViewController).GetPageIndex] 
    )

override x.GetNextViewController(pageViewController : UIPageViewController, contentController : UIViewController) = 
    let mutable returnController : UIViewController = null
    if flatImages.Length > 0 then 
        let curr_index = (contentController :?> FlatImagesContentViewController).GetPageIndex
        Console.WriteLine("Attempting after with index ")
        Console.WriteLine(curr_index.ToString())
        if curr_index < flatImages.Length - 1 then 
            returnController <- viewControllerAtIndex(curr_index + 1)
    returnController

override x.GetPreviousViewController(pageViewController : UIPageViewController, contentController : UIViewController) =
    let mutable returnController : UIViewController = null
    if flatImages.Length > 0 then 
        let curr_index = (contentController :?> FlatImagesContentViewController).GetPageIndex
        Console.WriteLine("Attempting before with index ")
        Console.WriteLine(curr_index.ToString())
        if curr_index > 0 then 
            returnController <- viewControllerAtIndex(curr_index - 1)
    returnController

override x.GetPresentationCount(pageViewController : UIPageViewController) =
    Conversions.nint(flatImages.Length)

override x.GetPresentationIndex(pageViewController : UIPageViewController) = 
    let mutable returnVal = 0
    if flatImages.Length > 0 then 
        returnVal <- (controllers.[0] :?> FlatImagesContentViewController).GetPageIndex
    Conversions.nint(returnVal)

此代码在该行崩溃:

parentView.SetViewControllers(viewControllers |> List.toArray,UIPageViewControllerNavigationDirection.Forward,true,null)

在此代码块中:

member this.GetFlatImages 
with get() = flatImages 
and set(value) = (
    flatImages <- value
    if not isinitialised then 
        parentView.View.UserInteractionEnabled <- true
        if flatImages.Length > 0 then 
            let initialcontroller = viewControllerAtIndex(0)
            let mutable viewControllers : UIViewController list = []
            viewControllers <- initialcontroller::viewControllers
            parentView.SetViewControllers(viewControllers |> List.toArray,UIPageViewControllerNavigationDirection.Forward,true,null)
            controllers <- viewControllers
        isinitialised <- true
    else
        ((controllers.[0]) :?> FlatImagesContentViewController).GetImage <- flatImages.[((parentView.ViewControllers.[0]) :?> FlatImagesContentViewController).GetPageIndex] 
)

有以下例外:

enter image description here

我不明白,因为我已经检查initialController绝对不是null,因此我不明白这个例外的来源。

1 个答案:

答案 0 :(得分:0)

我注意到当你想使用parentView.SetViewControllers(...)时,viewControllers只有一个对象。您如何构建UIPageViewController

如果您使用UIPageViewControllerSpineLocation.Mid或默认UIPageViewControllerSpineLocation(默认为中),则在设置其ViewControl时至少需要两个控件。

因此您可以尝试修改初始方法,如:

new UIPageViewController(UIPageViewControllerTransitionStyle.PageCurl, 
                         UIPageViewControllerNavigationOrientation.Horizontal,
                         UIPageViewControllerSpineLocation.Min)