简体   繁体   English

使用TableView的Xamarin IOS导航

[英]Xamarin IOS Navigation with TableView

I am sure this is probably a very simple solution, but I am quite new to xamarin and c# in general. 我敢肯定这可能是一个非常简单的解决方案,但对于xamarin和c#来说,我还是一个新手。 I have created a tableview, and when a cell in that view is pressed I want it to navigate to a new view, named SecondViewController(). 我创建了一个表格视图,当按下该视图中的一个单元格时,我希望它导航到一个名为SecondViewController()的新视图。 I have the following class in my ViewController.cs 我的ViewController.cs中有以下类

     public void changeview()
    {

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

Then in my TableSource.cs class, I have the following code to call changeview() 然后在TableSource.cs类中,我有以下代码调用changeview()

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

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

I get no errors when it compiles, but when it runs I get an error on the following code 编译时没有错误,但运行时出现以下代码错误

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

which reads 上面写着

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

why does this not work? 为什么这不起作用? if I use this same code within a button class it works great, to test to make sure I was calling the class correctly I changed the code within changeview() to a UIAlertView and when a cell was pressed the alertview worked. 如果在按钮类中使用相同的代码,则效果很好,为了进行测试以确保我正确地调用了该类,我将changeview()中的代码更改为UIAlertView,并且当按下单元格时,alertview起作用了。 I am not sure where to go from here, possibly there is a better method for changing views for this purpose? 我不知道从哪里去,可能有更好的方法来更改视图吗? any help is greatly appreciated! 任何帮助是极大的赞赏!

You will need to have a global reference or access to the current active navigation controller of your application in order to make a push. 为了进行推送,您将需要具有全局引用或对应用程序的当前活动导航控制器的访问权限。

What you can do is to create an instance of an AppDelegate and set the Navigation Controller to a variable. 您可以做的是创建AppDelegate的实例,并将Navigation Controller设置为变量。

In your AppDelegate.cs 在您的AppDelegate.cs中

[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    //application layer declarations
    public UINavigationController navCtrl { get; set; }
    /*
    .
    .  the rest of the codes
    .
    .*/
}

In your ViewController.cs (First view) or the root view controller where you instantiated the navigation controller: 在ViewController.cs(第一视图)或实例化导航控制器的根视图控制器中:

public override void ViewDidLoad() {
    AppDelegate appD = UIApplication.SharedApplication.Delegate as AppDelegate;

    // assign the UINavigationController to the variable being used to push
    // assuming that you have already initialised or instantiated a UINavigationController
    appD.navCtrl = this.NavigationController; // Your UINavigation controller should be here
    /*
    .
    . the rest of the codes
    .
    */
}

In your TableSource.cs Class. 在您的TableSource.cs类中。

private AppDelegate appD = UIApplication.SharedApplication.Delegate as AppDelegate;

public override void RowSelected (UITableView tableView, NSIndexPath indexPath){
    SecondViewController secondView = UIStoryboard.FromName ("Main", null).InstantiateViewController ("SecondViewController") as SecondViewController;
    appD.navCtrl.PushViewController (secondView, true);
}

This should be able to do what you want. 这应该可以做您想要的。

You should keep a reference of your ViewController in your TableSource.cs class as 您应该在TableSource.cs类中保留ViewController的引用为

public ViewController ParentController{ get; set;}

When initializing the data source, 初始化数据源时,

tableClassObject.ParentController = this;

In Row Selected, it should be 在“选择的行”中,应为

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

This is simplest solution. 这是最简单的解决方案。

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

Do you used Storyboard Id for "SecondViewController". 您是否将Storyboard ID用于“ SecondViewController”。 If not goto stroyboard add storyboard Id to your SecondViewController 如果没有,请添加故事板ID到您的SecondViewController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM