问题描述
我正在尝试在 MonoTouch 中使用容器视图,并且正在在线学习一些教程.他们谈论以编程方式从容器中添加和删除视图控制器.我在项目的故事板中创建了一个视图控制器和视图,并附加了一些插座和一个动作(分别用于标签和按钮).我创建了一个重载的结构
I am trying to work with a container view in MonoTouch and I am following some tutorials online. They talk about adding and removing view controller programmatically from the container. I created a viewcontroller and view in the storyboard of my project and attached a few outlets and one action (for labels and buttons respectively). I created an overloaded construc
这是视图控制器中的代码,我试图将视图控制器添加到容器视图中.
Here is the code in the view controller that I am trying to add viewControllers into the container view.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
ContainerView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight;
_controllerOne = new IngredientsController("Perishables");
_controllerTwo = new IngredientsController("Spices");
AddChildViewController(_controllerOne);
ContainerView.AddSubview(_controllerOne.View);
_controllerOne.DidMoveToParentViewController(this)
}
当我为 _controllerOne 添加子视图时,我收到一个错误,因为我的控制器上的元素被标记为空.如果控制器是在 Interface Builder 中制作的,MonoTouch 是否无法以编程方式创建视图控制器?下面是成分控制器的两个构造函数.当使用 segue 时,所有 UI 控件都会正确初始化.我是否需要以编程方式创建控制器然后以这种方式实例化它?任何帮助将不胜感激.
When I add the subview for _controllerOne I get an error because the elements on my controller are marked null. Is MonoTouch incapable of having view controllers being programmatically created if the controller was made in Interface Builder? Below are the two constructors for the Ingredient Controller. When the segue is used then all of the UI controls are initialized properly. Do I need to create the controller programmatically and then instantiate it that way? Any help would be appreciated.
//This ctor does not work
public IngredientsController (string title) : base(NSObjectFlag.Empty)
{
_ingredientTitle = title;
}
//This ctor works
public IngredientsController (IntPtr handle) : base (handle)
{
}
推荐答案
尝试像这样实例化视图控制器:
Try instantiating the view controller like this:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.ContainerView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight;
var newController = this.Storyboard.InstantiateViewController("IngredientsController");
this.AddChildViewController (newController);
this.ContainerView.AddSubview (mapController.View);
}
确保在 ViewController 的属性面板中设置 Storyboard Id
这篇关于MonoTouch 以编程方式为 ContainerView 实例化 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!