0

我正在使用 mono develop 3.1.1 来构建 IOS 应用程序。我从我没有正确声明的导航控制器的引用中收到一个对象引用错误(请参阅 >>>)。

我的问题是声明和实例化控制器的最佳方式是什么,以便我能够从选择表格单元格的点显示另一个视图。

有人可以帮我正确的语法吗?

public class TableHelper : UITableViewSource {
    protected string[] tableItems;
    protected string cellIdentifier = "TableCell";


    public TableHelper (string[] items)
    {
        tableItems = items;
    }




    public override int RowsInSection (UITableView tableview, int section)
    {
        return tableItems.Length;
    }


    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
    {
        switch (tableItems[indexPath.Row])
        {
        case "one": 
            var DetailViewController = new SupportContactsDetailsScreen ();
            UINavigationController controller = new UINavigationController();
            // Pass the selected object to the new view controller.
            >>>controller.NavigationController.PushViewController(DetailViewController, true);
            break;
        default:
            //Console.WriteLine("Default case");
            break;
        }
    }


    public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {

        UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);

        if (cell == null)
            cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);

        cell.TextLabel.Text = tableItems[indexPath.Row];

        return cell;
    }
}
4

1 回答 1

0

我通常这样做的方法是保留对特定视图集的主 UIViewController(保存 UITableView 的视图控制器)的引用,并通过 NavigationController 属性访问该导航控制器。(Xamarin 在下面链接的代码示例中采用的另一种技术是直接传递 UINavigationController。)

所以我会通过添加来改变你的班级:

UIViewController parentViewController;
public TableHelper(string[] items, UIViewController vc)
{
    tableItems = items;
    parentViewController vc;
}

public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
    switch (tableItems[indexPath.Row])
    {
    case "one": 
        var DetailViewController = new SupportContactsDetailsScreen ();
        UINavigationController controller = new UINavigationController();
        // Pass the selected object to the new view controller.
        parentViewController.NavigationController.PushViewController(DetailViewController, true);
        break;
    default:
        //Console.WriteLine("Default case");
        break;
    }
}

Xamarin在他们的文档网站上有一个文档,在他们的Github上有一些代码进一步讨论了这个问题。另一个重要的注意事项是视图控制器的类型是什么(常规的 UIViewController、UITableViewController 等)。

于 2013-02-09T07:11:57.850 回答