本文介绍了如何在 RowSelected 事件上导航到 Xamarin iOS 中的 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主屏幕上有一个 TableView,它位于导航控制器内.Now, when a row is selected, I want to show a MapView.

I am having a TableView on my home screen which is inside a Navigation Controller. Now, when a row is selected, I want to show a MapView.

我想访问导航控制器并将 MapViewController 推入其中.我怎样才能做到这一点?

I want to get access to the Navigation Controller and push a MapViewController into it. How can i achieve this?

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

}

推荐答案

我假设你的 RowSelected 方法在你的 UITableViewController 中,对吧?在这种情况下,这很容易,因为您可以访问自动设置为父 UINavigationControllerNavigationController 属性(在 UIViewcontroller 中定义)>

I assume your RowSelected method is in your UITableViewController, right? In this case, it's easy, as you can access the NavigationController property (defined in UIViewcontroller) which is automatically set to the parent UINavigationController

public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
    var index = indexPath.Row;
    NavigationController.PushViewController (new MyDetailViewController(index));
}

现在,您可能应该使用 UITableViewSource,并在那里覆盖 RowSelected.在这种情况下,通过构造函数注入确保 UINavigationController 可用:

Now, you probably should use a UITableViewSource, and override RowSelected there. In that case, make sure the UINavigationController is available by doing constructor injection:

tableViewController = new UITableViewController();
tableViewController.TableView.Source = new MyTableViewSource (this);

class MyTableViewSource : UITableViewSource
{
    UIViewController parentController;
    public MyTableViewSource (UIViewController parentController)
    {
        this.parentController = parentController;
    }

    public override int RowsInSection (UITableView tableview, int section)
    {
        //...
    }

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        //...
    }

    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
    {
        var index = indexPath.Row;
        parentController.NavigationController.PushViewController (new MyDetailViewController(index));
    }
}

将这个通用答案中的 MyDetailViewController 替换为您的 MapViewController,您应该已准备就绪.

Replace MyDetailViewController in this generic answer by your MapViewController and you should be all set.

这篇关于如何在 RowSelected 事件上导航到 Xamarin iOS 中的 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 11:25
查看更多