MonoTouch 如何自动生成 XIB 代码?
我是一名 C# 程序员,正在尝试使用 MonoTouch 进行 iPhone 开发。
我将一个新的视图界面定义添加到我的项目中,然后双击在界面生成器中将其打开。我添加了一个 UIButton。我保存文件,并检查 xib.designer.cs 文件,我看不到对新按钮的引用。
我从 http://monotouchexamples.com/ 下载了代码,在那里我可以看到后面自动生成的代码的示例:
[MonoTouch.Foundation.Connect("infoButton")]
private MonoTouch.UIKit.UIButton infoButton {
get {
return ((MonoTouch.UIKit.UIButton)(this.GetNativeField("infoButton")));
}
set {
this.SetNativeField("infoButton", value);
}
}
我在界面生成器中打开 MainWindow.xib。我注意到一些差异。文件的所有者是 UIApplication 类型而不是 NSObject 类型。这有什么重要意义呢?有一个 AppDelegate 类型的 App Delegate 对象。我无法将 AppDelegate 添加到我自己的视图中,或者至少我在库中找不到它。我需要添加一个吗?我可以看到 MainWindow.xib 上的现有控件具有指向应用程序委托的引用出口。我添加了一个新按钮,我想将其连接起来。当我单击新引用插座并将其拖动到应用程序委托时,会出现一个上下文菜单,其中列出了现有控件。如何向此列表添加新元素,或者此列表来自哪里?
我已经被 Visual Studio 世界宠坏了,我只是在窗体上转储一个按钮,然后开始为单击事件编写代码。有人可以提供一些关于在 MonoTouch 上实现此功能所需的步骤的指示吗?
谢谢,
帕特里克
I'm a C# programmer dabbling in a bit of iPhone development using MonoTouch.
I add a new View Interface Definition to my project and double click to open it up in Interface Builder. I add a UIButton. I save the file, and inspect the xib.designer.cs file, and I can see no reference to the new button.
I downloaded the code from http://monotouchexamples.com/ where I could see an example of autogenerated code behind :
[MonoTouch.Foundation.Connect("infoButton")]
private MonoTouch.UIKit.UIButton infoButton {
get {
return ((MonoTouch.UIKit.UIButton)(this.GetNativeField("infoButton")));
}
set {
this.SetNativeField("infoButton", value);
}
}
I opened up MainWindow.xib in interface builder. I notice a few differences. File's Owner is of type UIApplication instead of NSObject. What is the importance of this? There is an App Delegate object of type AppDelegate. I can't add an AppDelegate to my own view, or at least I can't find it in the Library. Do I need to add one? I can see that the existing controls on MainWindow.xib have Referencing Outlets to the App Delegate. I add a new button and I want to hook it up. When I click and drag a New Referencing Outlet to the App Delegate a context menu appears that lists the existing controls. How do I add a new element to this list, or where does this list come from?
I've been spoilt by the Visual Studio world where I just dump a button on a form and start writing code for the click event. Could someone provide some pointers about the steps needed to get this working on MonoTouch?
Thanks,
Patrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
单独添加一个按钮是不够的。在 Interface Builder 之外无法访问该按钮。您需要添加一个插座,并将按钮与 Interface Builder 中的插座连接。
请记住:Outlet 是 Controller 类中获取控件引用的成员,如果没有它们,您将无法访问控件。
Adding a button by itself is not enough. The button is not accessible outside the Interface Builder. You need add an Outlet, and connect the button with the outlet in Interface Builder.
Remember: Outlets are the members in your Controller class that get a reference to the controls, you can't just access the controls without them.
正如戴夫所说,您需要在控制器类中添加一个插座,并将按钮连接到该插座,然后才会出现任何自动生成的代码。一开始这也让我感到困惑。
您在 Interface Builder 库窗口中选择控制器类,在库底部选择“插座”,然后在其中添加一个插座。然后,您需要选择按钮,选择检查器窗口的连接选项卡,然后从“新引用插座”圆圈拖动到控制器类。然后,Interface Builder 将提示您选择要连接的插座。然后,当您保存时,自动生成的代码应该出现在 .xib.designer.cs 文件中,然后您应该能够通过 .xib.cs 文件中的出口变量引用您的按钮。
As Dave says, you need to add an outlet to your controller class, and connect your button to that outlet, before any auto-generated code will appear. This caught me out too initially.
You choose your controller class in the Interface Builder library window, choose 'outlets' in the bottom part of the library, and add an outlet there. You then need to select your button, choose the connections tab of the inspector window, and drag from the "New referencing outlet" circle over to your controller class. Interface Builder will then prompt you to choose an outlet to connect to. Then when you save, you should get the auto-generated code appear in the .xib.designer.cs file, and then you should be able to reference your button via that outlet variable in your .xib.cs file.
听起来我创建的项目已经过时了 - 我记得关于如何在设计器文件中创建生成的按钮有相当多的更改。我会尽快为您更新该项目。
正如 Dave 所说,要自动生成代码,您需要使用 Interface Builder 添加一个插座。这里应该有一个关于此视频的示例 - http://bit.ly/aWoItN 但服务器似乎此刻心情低落。
希望这有帮助,
克里斯NTR
It sounds like the project I created is out of date - I remember there were quite a few changes around how the generated buttons are created in the designer file. I will update the project soon for you.
As Dave said, to get the code to be auto generated you need to add an outlet with Interface Builder. There should be an example on this video here - http://bit.ly/aWoItN but the server seems to be down at the moment.
Hope this helps,
ChrisNTR