c# - 如何在 MonoTouch 中实现 IBOutlets?

标签 c# iphone xamarin.ios iboutlet

我用 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 开始,如下所示: step 1
(来源:jamespwright.com)

步骤 2) 将 UILabel 控件添加到您的 View : step 2
(来源:jamespwright.com)

第 3 步) 选择“文件所有者”: step 3
(来源:jamespwright.com)

Step 4) 选择 Identity Inspector (CMD + 4) 并添加一个新的 Class Outlet: step 4
(来源:jamespwright.com)

第 5 步) 选择 Connections Inspector(CMD + 2),您应该会在那里看到新的 Class Outlet: step 5
(来源:jamespwright.com)

步骤 6) 将连接拖到 View 上的标签。

步骤 7) 在您的代码中,您通过键入 myLabel.Text = "My Label!"

来填充它

希望这会在将来帮助其他人。

关于c# - 如何在 MonoTouch 中实现 IBOutlets?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2277836/

相关文章:

c# - Windows.Form ComboBox 无法设置未绑定(bind)控件的 SelectedValue 属性

c# - 在MVC4中,如何一次保存多行编辑?

iphone - 日期选择器 IOS 每月重复提醒

iPhone - 更改 View 后面的 UIWebView 背景颜色(弹跳背景)

c# - AnyCPU C# DLL in 64bit process is unable to refer 64 bit C++ DLL(Giving error : Could not load file or assembly)

c# - 将父模型的一个元素传递给局部 View

ios - 检测设备是否为 iPhone 5s

c# - 如何在 Xamarin.iOS 中实现 +(Class) layerClass?

c# - 订阅和取消订阅单元格按钮单击事件在 iOS 11 中不起作用

c# - 将邮件编码为 : uri