我用 Objective-C 开发了一个 iPhone 应用程序。当我想将标签链接到某些数据时,我会这样声明它:
@interface CityDetailViewController : UIViewController {
UILabel *cityName;
}
@property(nonatomic, retain) IBOutlet UILabel *cityName;
然后当在代码中创建 CityDetailViewController 对象时,我会像这样设置城市名称 [self.cityView.cityName setText:city.name];
我这辈子都想不出如何在 MonoTouch 中做到这一点。 我尝试通过 Interface Builder 手动创建 Outlets,并尝试添加我在另一个项目的 .designer.cs 文件中找到的代码:
[MonoTouch.Foundation.Connect("headlineLabel")]
private MonoTouch.UIKit.UILabel headlineLabel {
get {
return ((MonoTouch.UIKit.UILabel)(this.GetNativeField("headlineLabel")));
}
set {
this.SetNativeField("headlineLabel", value);
}
}
我已经尝试了这两种方法的组合。 他们不工作。 我能得到的最接近的是使用 Interface Builder 实际应用 Outlet,但是当我的 View 被声明时我得到这个错误:
this class is not key value coding-compliant for the key headlineLabel.
所以我完全不知所措。 有人可以给我解释一下吗?
最佳答案
很确定我已经弄明白了,所以我会把过程贴在这里供其他人查看:
第 1 步) 从您的空 View 开始,如下所示:
(来源:jamespwright.com)
步骤 2) 将 UILabel 控件添加到您的 View :
(来源:jamespwright.com)
第 3 步) 选择“文件所有者”:
(来源:jamespwright.com)
Step 4) 选择 Identity Inspector (CMD + 4) 并添加一个新的 Class Outlet:
(来源:jamespwright.com)
第 5 步) 选择 Connections Inspector(CMD + 2),您应该会在那里看到新的 Class Outlet:
(来源:jamespwright.com)
步骤 6) 将连接拖到 View 上的标签。
步骤 7) 在您的代码中,您通过键入 myLabel.Text = "My Label!"
希望这会在将来帮助其他人。
关于c# - 如何在 MonoTouch 中实现 IBOutlets?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2277836/