gpt4 book ai didi

objective-c - Xcode OSX,将 NSString 绑定(bind)到 UILabel

转载 作者:行者123 更新时间:2023-12-03 17:51:33 25 4
gpt4 key购买 nike

我有一个像这样的模型类:

标题:

@interface RTSecurityModel : NSObject
{
NSString *code;
}

@property NSString *code;

@end

实现:

@implementation RTSecurityModel

@synthesize code;

@end

然后我就有了我的应用程序代理:

标题:

@interface RTAppDelegate : NSObject <NSApplicationDelegate>
{
RTSecurityModel *security;
}

@property (assign) IBOutlet NSWindow *window;

@property RTSecurityModel *security;

@end

实现:

@implementation RTAppDelegate

@synthesize security;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
security = [[RTSecurityModel alloc] init];
security.code = @"test";
}

然后在我的 MainMenu.xib 中创建一个标签,并在绑定(bind)检查器中设置“绑定(bind)到:应用程序委托(delegate)”和“模型 key 路径:security.code”。

但是当我开始我的应用程序时没有任何显示。

我尝试了很多方法来绑定(bind)这个变量,但没有人成功。

请帮助我不要讨厌 XCode 和 Cocoa!

UPD:http://www.experts-exchange.com/Programming/Languages/C/A_3381-Simple-Binding-Cocoa-GUI-Application-without-Outlets.html

以下是如何通过编辑文本字段设置属性和标签值的示例

但是有没有一种方法可以在不编辑文本字段的情况下编辑标签?或者根本没有文本字段?

UPD2:

您不得创建另一个对象实例

security = [[RTSecurityModel alloc] init]; // Kill this

非常感谢维克托·列克星敦

最佳答案

不要使用 security.code 作为模型路径,而是使用 code。在绑定(bind)选项卡的值部分使用类 RTSecurityModel 而不是 AppDelegate。

Here是一个演示项目。

请勿绑定(bind)文本字段单元格,而是使用文本字段

如果您用文本填充Null Placeholder,您可以检查值是否为空,它会显示该文本吗?然后在绑定(bind)值时将其设置为空。

要在 Interface Builder 中查看您的 RTSecurityModel,您必须让它知道您的类,它不会查找它。

添加一个对象,然后将其自定义类设置为RTSecurityModel。然后您可以选择该对象并将引用 socket 设置为App Delegate中的属性。分配现在将直接反射(reflect)在标签中。

enter image description here

<小时/>

我可以想出两种无需 Interface Builder 以编程方式解决此问题的方法:

  1. Key Value Coding

    // add an observer for the value on the object that has the method below implemented
    [self addObserver: self forKeyPath: @"security.code" options: NSKeyValueObservingOptionNew context: NULL];

    // method will be called when the observer has 'seen' a value change
    -(void) observeValueForKeyPath: (NSString *)keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context {
    label.text = ...
    }
  2. code 使用自定义 setter(@synthesize 仍将为您创建 getter)

    - (void)setCode:(NSString *)aString {
    label.text = aString;
    }

关于objective-c - Xcode OSX,将 NSString 绑定(bind)到 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25230510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com