从 Xamarin.iOS 中的 TableView 单元格以编程方式推送 segue [英] Push a segue programatically from a TableView Cell in Xamarin.iOS
问题描述
我目前正在使用 Xamarin 开发一个相当简单的应用程序,但我遇到了一件事:我在 UIViewController 中创建了一个 tableView.单元格渲染在外部类中完成:TableSource.cs.UITableViewSource 的一个子类.要处理行单击我确实覆盖了选定的行:
i am developing a rather simple App with Xamarin at the moment, but i am stuck with one thing: I created a tableView within a UIViewController. The cell rendering is done in an external class: TableSource.cs. A Subclass of UITableViewSource. To handle the row Click i did override Row selected:
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 以显示有关该行的详细数据时,什么也没有发生,我没有收到任何错误或异常消息.我还尝试使用用于导航的自定义类.控制器和视图控制器,它不会改变任何东西.
Works like charm so far. But when i try to push another ViewController on top, to display detailed data about the row, just nothing happens, i don't get any error or exception messages. I also tried it with custom classes for the Nav. Controller and the view Controller, it doesn't change anything.
推荐答案
你不能只是创建一个新的 UINavigationController 并像这样使用它.相反,您需要在创建 TVC 时将 UITableViewController 包装在 UINavigationController 中 - 然后在您的 RowSelected 中,您可以使用现有的 NavigationController 将新的 ViewController 推送到堆栈上.
You can't just create a new UINavigationController and use it like that. Instead, you need to wrap your UITableViewController in a UINavigationController when you create the TVC - then in your RowSelected you can use the existing NavigationController to push the new ViewController on the stack.
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);
}
在创建 TableSource 时,传入对 TableViewController 的引用,以便源的 RowSelected 方法可以访问 NavigationController.
When you create your TableSource, pass in a reference to the TableViewController so that your source's RowSelected method can access the 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
Now, in your RowSelected method, you can access parent.NavigationController
这篇关于从 Xamarin.iOS 中的 TableView 单元格以编程方式推送 segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!