c# - 使用正确的构造函数创建 View Controller 的实例

标签 c# ios uiviewcontroller constructor xamarin.ios

教程中Use Split View to Show two Controllers您以这种方式创建 View Controller 的实例:

masterView = new MasterViewController ();

因此你需要这样的构造器:

public MasterViewController () : base()

我目前的问题是我的 MasterViewController 没有加载。如果我运行该应用程序,会显示一个空表,但我的表中应该有一些自定义项和数据。

现在我正在使用自定义单元格。在教程中Part 2 - Populating a Table with Data明确指出:

Be aware, when using the new reuse pattern with a custom cell class, you need to implement the constructor that takes an IntPtr, as shown in the snippet below, otherwise Objective-C won't be able to construct an instance of the cell class

public MasterViewController (IntPtr ptr) : base (ptr)

如果我将我的构造函数更改为此我得到

The type MasterViewController does not contain a constructor that takes 0 arguments.

当我尝试实例化 View Controller 时。

如何使用正确的构造函数实例化 View Controller ?

最佳答案

现在我在我的 View Controller 的构造函数中使用以下模式:

public MasterViewController () : base()
{
    initialize();
}

public MasterViewController (IntPtr ptr) : base (ptr)
{
    initialize();
}

private initialize()
{
    // do your initialization here
}

这种方法的优点是您可以从 Storyboard 中实例化事物,也可以使用 new 关键字在代码中直接实例化它。

不知道我的自定义单元格问题的解决方案是什么。希望这会有所帮助。

关于c# - 使用正确的构造函数创建 View Controller 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25780462/

相关文章:

c# - 如何在 C#/XAML 中的单个 Storyboard中制作多个动画?

C# 使用序列化将对象附加到 xml 文件

c# - 序列化 JSON 随机名称

ios - 防止 UIPopover 上的动画被外面的点击取消?

ios - 在 swift 2.3 中使用 Alamofire 调用 Web 服务时出错

ios - 标签栏中的 View Controller (调用另一个 vc 作为弹出窗口),在切换标签时从标签栏层次结构中删除

c# - 在特定时间启动和停止 MediaElement

android - Kivy 应用程序需要 30 秒才能打开

swift - 在 Swift 中使用容器 View 进行委托(delegate)

ios - 如何在 subview Controller 中更改 iOS 状态栏颜色