【问题标题】:How to access objects from different classes in Cocoa Programming如何在 Cocoa 编程中访问不同类的对象
【发布时间】:2013-03-04 17:06:31
【问题描述】:

我有一个 NSTextField 子类(称为“txtField1”并在我的界面构建器中用作文本字段的自定义类),我希望能够从此类访问我的界面构建器中存在的 NSComboBox 对象。

这是我的代码: txtField1.h:

#import <Cocoa/Cocoa.h>

@interface txtField1 : NSTextField

@end

txtField.m:

#import "txtField1.h"

@implementation txtField1

-(void)mouseDown:(NSEvent *)theEvent
{
    HERE I would like to be able to write something like:
    [combobox SetHidden:YES];
}

@end

我希望能够在 mouseDown 事件中设置访问组合框 SetHidden 属性。 你能告诉我怎么做吗?我尝试了在互联网上找到的不同解决方案,但根本没有得到任何东西! 任何帮助将不胜感激。

【问题讨论】:

标签: objective-c xcode macos cocoa iboutlet


【解决方案1】:

这里有很多方法和答案:

Update a label through button from different view

Xcode - update ViewController label text from different view

Setting label text in another class

Set label on another view to stored NSDate

编辑:

-(void)mouseDown:(NSEvent *)theEvent
{
    HERE I would like to be able to write something like:
    [combobox SetHidden:YES];
    /*
        use the shared instance of comboBox here and make it hidden.
        Also, you can use binding to make it hidden
    */
}

【讨论】:

  • 对不起,但实际上你建议的答案都没有帮助我。你能举个例子吗?
  • access an NSComboBox object which present in my interface builder from this class.在哪个班级?
  • "在这里使用comboBox的共享实例并使其隐藏。"由于我是可可编程的新手,您能否给我举个代码示例?谢谢。
  • 有什么想法吗?我将尝试向您解释我正在尝试做的所有事情:我有一个 NSTextfield 和一个 NSComboBox。好吧,当有人在 NSTextField 内单击时,我希望 NSTextField 消失并且 NSComboCox 出现。帮我。谢谢。
【解决方案2】:

从我的角度来看,txtField1 类并不是这段代码更好的地方。

您可以将NSControlTextEditingDelegate 协议添加到您的NSViewController 实现(对于txtField1combobox 已经包含IBOutlets)并在方法– control:textView:doCommandBySelector: 中实现隐藏您的NSComboBox

【讨论】:

  • 有什么想法吗?我将尝试向您解释我正在尝试做的所有事情:我有一个 NSTextfield 和一个 NSComboBox。好吧,当有人在 NSTextField 内单击时,我希望 NSTextField 消失并且 NSComboCox 出现。帮我。谢谢。
猜你喜欢
  • 2018-03-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多