简体   繁体   中英

Unable to load ViewController from tableview RowSelected in xamarin

On selected tableview rowselected loading a DetailViewcontroller class.

    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            if (OnRowSelected != null) {
                OnRowSelected (this, new RowSelectedEventArgs (tableView, indexPath));
            }

                var detailController = new DetailViewController ();
                    UINavigationController controller = new UINavigationController();
                    controller.PushViewController(detailController, true);
}

Unable to load the DetailViewController.

public override void RowSelected (UITableView tableView, NSIndexPath indexPath) 

method is in UITableViewSource

Is there any way to load DetailViewController on rowSelected.

When you create your table, you need to place it within the context of a Navigation controller.

UINavigationController nav = new UINavigationController(myTableViewController);

Then, when you want to display your DetailViewController, you can push it onto the already existing Navigation controller. However, by default your TableSource does not have access to NavigationController - you will need to pass a reference to your TableView controller when you create the TableSource so that the TableSource can access the NavigationController:

// in your controller, when you assign the source
this.TableView.Source = new MyTableViewSource(this);

// in your source, keep a class level ref to the parent
MyTableViewController _parent;

// your Source's constructor
public MyTableViewSource(MyTableViewController parent) {
  _parent = parent;
}

// finally, in your RowSelected use the _parent reference to access the Nav controller
var detailController = new DetailViewController ();
_parent.NavigationController.PushViewController(detailController, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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