使用TableView进行Xamarin IOS导航

时间:2015-12-20 16:49:31

标签: c# uitableview xamarin uinavigationcontroller

我确信这可能是一个非常简单的解决方案,但我对xamarin和c#一般都是新手。我创建了一个tableview,当按下该视图中的一个单元格时,我希望它导航到一个名为SecondViewController()的新视图。我的ViewController.cs中有以下类

     public void changeview()
    {

           SecondViewController controller = this.Storyboard.InstantiateViewController("SecondViewController") as SecondViewController;
        this.NavigationController.PushViewController(controller, true);
    }

然后在我的TableSource.cs类中,我有以下代码来调用changeview()

   public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {

        ViewController view = new ViewController();
        view.changeview();
        tableView.DeselectRow(indexPath, true);
    }

编译时没有错误,但是当它运行时,我在下面的代码中得到错误

   SecondViewController controller = this.Storyboard.InstantiateViewController("SecondViewController") as SecondViewController;

读取

  System.NullReferenceException: Object reference not set to an instance of an object

为什么这不起作用?如果我在按钮类中使用相同的代码它工作得很好,测试以确保我正确地调用类我将changeview()中的代码更改为UIAlertView并且当按下单元格时,alertview工作。我不知道从哪里开始,可能有更好的方法来改变这个目的的观点?非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

您应该在TableSource.cs类中将ViewController的引用保留为

public ViewController ParentController{ get; set;}

初始化数据源时,

tableClassObject.ParentController = this;

在选定的行中,它应该是

public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        tableView.DeselectRow(indexPath, true);
        ParentController.changeview();
    }

这是最简单的解决方案。

答案 1 :(得分:0)

       SecondViewController controller = this.Storyboard.InstantiateViewController("SecondViewController") as SecondViewController;

您是否使用了Storyboard Id以及#34; SecondViewController"。如果没有goto stroyboard将storyboard Id添加到你的SecondViewController