[英]C#(Xamarin iOS) How to navigate from UITableView to other ViewController?
[英]How to navigate to ViewController in Xamarin iOS on RowSelected event
我在主屏幕上有一个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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.