如何在RowSelected事件中导航到Xamarin iOS中的ViewController
我在主屏幕上有一个TableView,它位于导航控制器内。 现在,当选择一行时,我想显示一个MapView。
我想访问导航控制器并将MapViewController推入其中。 我怎样才能做到这一点?
public override void RowSelected (UITableView tableView, NSIndexPath indexPath) { }
我假设你的RowSelected
方法在你的UITableViewController
,对吧? 在这种情况下,它很容易,因为您可以访问NavigationController
属性(在UIViewcontroller
定义),该属性自动设置为父UINavigationController
public override void RowSelected (UITableView tableView, NSIndexPath indexPath) { var index = indexPath.Row; NavigationController.PushViewController (new MyDetailViewController(index)); }
现在,您可能应该使用UITableViewSource
,并RowSelected
那里覆盖RowSelected
。 在这种情况下,请确保通过构造函数注入使UINavigationController
可用:
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)); } }
通过MapViewController
替换此通用答案中的MyDetailViewController
,您应该全部设置。
我想从IndexViewController导航到ViewController。 我使用以下代码。
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); }