我在导航控制器内的主屏幕上有一个 TableView。Now, when a row is selected, I want to show a MapView.
我想访问导航控制器并将 MapViewController 推入其中。我怎样才能做到这一点?
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
}
我在导航控制器内的主屏幕上有一个 TableView。Now, when a row is selected, I want to show a 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
。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));
}
}
用您的通用答案替换MyDetailViewController
此通用答案,MapViewController
您应该一切就绪。
我想从 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);
}