从Xamarin.iOS中的TableView Cell以编程方式推入一个segue

我正在用Xamarin开发一个相当简单的应用程序,但是我陷入了一件事:我在UIViewController中创build了一个tableView。 单元格渲染是在外部类中完成的:TableSource.cs。 UITableViewSource的一个子类。 处理行单击我重写行select:

public override void RowSelected (UITableView tableView, NSIndexPath indexPath) { new UIAlertView("Row Selected", tableItems[indexPath.Row], null, "OK", null).Show(); tableView.DeselectRow (indexPath, true); // iOS convention is to remove the highlight //Push another ViewController on top: UINavigationController test = new UINavigationController (); test.PushViewController (new UIViewController (),true); } 

像迄今为止的魅力。 但是当我尝试推顶另一个ViewController,显示关于该行的详细数据,只是没有任何反应,我没有得到任何错误或exception消息。 我也用导航的自定义类来尝试它。 控制器和视图控制器,它不会改变任何东西。

你不能只是创build一个新的UINavigationController并使用它。 相反,当您创buildTVC时,您需要将UITableViewController包装在UINavigationController中 – 然后在您的RowSelected中,可以使用现有的NavigationController将新的ViewController推入堆栈。

 public override void RowSelected (UITableView tableView, NSIndexPath indexPath) { new UIAlertView("Row Selected", tableItems[indexPath.Row], null, "OK", null).Show(); tableView.DeselectRow (indexPath, true); // iOS convention is to remove the highlight // parent is a reference to the UITableViewController - you will need to pass this into // your TableSource class. Every ViewController has a NavigationController property - if // the VC is contained in a Nav controller this property will be set. parent.NavigationController.PushViewController(new UIViewController(), true); } 

在创buildTableSource时,传入对TableViewController的引用,以便源代码的RowSelected方法可以访问NavigationController。

 // in your TableViewController, when you assign the Source property TableView.Source = new MyTableSource(this); // in the MyTableSource class TableViewController parent; public MyTableSource(TableViewController parent) { this.parent = parent; } 

现在,在你的RowSelected方法中,你可以访问parent.NavigationController

 UINavigationController test=new UINavigationController (); this.NavigationController.PushViewController(test,true);