How to navigate to ViewController in Xamarin iOS on RowSelected event

血红的双手。 提交于 2019-12-13 15:13:11

问题


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.

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)
{

}

回答1:


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

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

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




回答2:


I wanted to navigate from IndexViewController To ViewController. I use the following code.

IndexViewController owner;

    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
            {
                UIStoryboard board = UIStoryboard.FromName ("Main", null);
                ViewController ctrl = (ViewController)board.InstantiateViewController ("viewControllerID");
                owner.NavigationController.PushViewController (ctrl, true);
        }


来源:https://stackoverflow.com/questions/19443303/how-to-navigate-to-viewcontroller-in-xamarin-ios-on-rowselected-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!