通过 monotouch 绑定在 Interface Builder 中创建为 UIView 的自定义控件
使用 monotouch 和 monodevelop,我想创建一个自定义控件。 我按照以下步骤操作:
- 添加一个新文件作为“iPhone View”(称为 TestView),
- 在 Interface Builder 中编辑 TestView.xib,es。更改其背景和大小
- 在 Interface Builder 中编辑 MainWindow.xib,添加 UIView 并将其类标识设置为“TestView”。
此时,我想启动应用程序并在 MainWindow 的 UIView 中查看 TestView 实例的内容。
为了获得这种“绑定”,我尝试了几个步骤(我知道它可以通过创建插座等的代码来完成,但我想了解是否可以通过界面生成器来完成)。
在尝试的方法之一中,我通过 Interface Builder 将值“TestView”设置为 TestView.xib 视图中的类标识符:这样 MonoTouch 在 TestView.xib.designer.cs 中创建了代码。
[MonoTouch.Foundation.Register("TestView7")]
public partial class TestView7 {
}
然后,在 TestView.xib.cs 中,我添加了:
public partial class TestView : UIView
{
public TestView (IntPtr p) : base(p)
{
}
}
当我启动应用程序时,我无法在 MainWindow 的视图中看到 TestView 的内容,但是如果在 TestView 构造函数中我添加一些内容,例如
BackgroundColor = UIColor.Yellow;
然后,我可以在 MainWindow 中看到 TestView...或者更好的是我只能看到黄色矩形,而不是真正的内容!
所以,问题是 TestView 绑定到 MainWindow 中的 UIView,但它的内容仅来自代码,而不是来自 TestView.xib 中通过 Interface Builder 定义的内容。
有没有办法从 TestView.xib 加载内容?
Using monotouch and monodevelop, I'd like to create a custom control.
I followed this steps:
- add a new file as "iPhone View" (calling it TestView)
- edit TestView.xib in Interface Builder, es. changing it's background and size
- edit MainWindow.xib in Interface Builder, adding a UIView and setting it's class identity as "TestView".
At this point, I'd like to launch the application and see in the UIView of MainWindow the content of an instance of TestView.
In order to get this "binding" I tried several steps (I know it can be done via code creating the outlets, etc.., but I'd like to understand if it can be done via Interface builder).
In one of the methods tried, I set via Interface Builder the value "TestView" as class identifier in the View of TestView.xib: in this way MonoTouch created the code in TestView.xib.designer.cs.
[MonoTouch.Foundation.Register("TestView7")]
public partial class TestView7 {
}
Then, in TestView.xib.cs, I added:
public partial class TestView : UIView
{
public TestView (IntPtr p) : base(p)
{
}
}
When I launch the app, I cannot see in the view of MainWindow the content of TestView, but if in TestView constructor I add something such as
BackgroundColor = UIColor.Yellow;
then, I can see TestView in MainWindow... or better I can see only the yellow rectangle, not the real content!
So, the problem is that TestView is binded to the UIView in MainWindow, but it's content comes only from the code and not from the content defined via Interface Builder in TestView.xib.
Is there a way to load the content from the TestView.xib?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下 http://ios.xamarin.com/Documentation/API_Design#Interface_Builder_Outlets_and_C.23
我相信您需要做的是添加一个处理 nib 文件的构造函数(以下来自上面的链接):
当使用 MonoTouch 时,您的应用程序将需要创建一个派生自 UIViewController 的类并像这样实现它:
然后要从 NIB 文件加载 ViewController,请执行以下操作:
这将从 NIB 文件加载用户界面。
Take a look at http://ios.xamarin.com/Documentation/API_Design#Interface_Builder_Outlets_and_C.23
I believe what you need to do is add a constructor that handles the nib file (The following is from the above link):
When using MonoTouch your application will need to create a class that derives from UIViewController and implement it like this:
Then to load your ViewController from a NIB file, you do this:
This loads the user interface from the NIB.
Allison A 在 SO 问题中的回答 monotouch - 可重用的 iOS 自定义视图 ,解释了如何将 Interface Builder 中创建的现有复合视图加载到自定义视图中。
The answer by Allison A in SO question monotouch - reusable iOS custom view, explains how to load existing composite views created in Interface Builder into a custom view.