【问题标题】:Custom NSView with outlets带有网点的自定义 NSView
【发布时间】:2013-11-03 17:11:43
【问题描述】:

我是 Mac 应用程序的新手,正在编写一个简单的应用程序,该应用程序的不同部分具有通用布局。它基本上是一个在所有部分都有一个或两个按钮(标题不断变化)的图像。

所以我想在一个新的 Nib 文件和一个单独的类文件(MyCustomView,它是 NSView 的子类)中创建一个带有一个 Image Well 和两个圆形按钮的 CustomNSView,它将在 initWithframe 中加载这个 Nib方法。所以现在当我拖放一个自定义视图并将其类设置为MyCustomView 时,我会立即获得图像和两个按钮,而无需任何额外的代码。但是现在我将如何控制其他视图控制器中的这些按钮(插座/操作)?每个地方都会使用相同的视图,所以我不能将 nib 中的文件所有者设置为视图控制器?

这样做对吗?有没有办法创建一个自定义视图,它将所有按钮操作委托给它包含的视图控制器?

【问题讨论】:

  • 希望我能在这个问题上设置一个赏金。我真的很想知道它是否可能。

标签: macos cocoa nsview


【解决方案1】:

您可以编写自定义委托。虽然使用它,您可以将消息从一个对象发送到另一个对象

【讨论】:

  • 我不明白这是如何回答这个问题的。
【解决方案2】:

我会这样做。我不会创建 CustomNSView,我会创建一个 CustomViewController(包括它的 xib 文件)。在那个 CustomViewController 上,我会设计两个按钮并像这样设置 CustomViewController.h。

@property (nonatomic, weak) id delegate; // Create a delegate to send call back actions
-(IBAction)buttonOneFromCustomVCClicked:(id)sender;
-(IBAction)buttonTwoFromCustomVCClicked:(id)sender;

CustomViewController.m 就是这样。

-(void)buttonOneFromCustomVCClicked:(id)sender {
    if ([self.delegate respondsToSelector:@selector(buttonOneFromCustomVCClicked:)]) {
        [self.delegate buttonOneFromCustomVCClicked:sender];
    }
}

-(void)buttonTwoFromCustomVCClicked:(id)sender {
    if ([self.delegate respondsToSelector:@selector(buttonTwoFromCustomVCClicked:)]) {
    [self.delegate buttonTwoFromCustomVCClicked:sender];
    }
}

在 customViewController 的界面构建器中,将两个按钮的 SentAction 事件链接到这两个方法(它们应该显示在 file's owner 中)。

然后在要加载通用自定义视图的 otherClass 中,像这样实例化通用视图控制器。

#import "customViewController.h"

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    customViewController *newCustomViewController = [[ViewController alloc] initWithNibName:@"customViewController" bundle:nil];
    [newCustomViewController setDelegate:self];

    self.backGroundView = [newCustomViewController view]; // Assuming **backGroundView** is an image view on your background that will display the newly instantiated view
}

-(void)buttonOneFromCustomVCClicked:(id)sender {
    // Code for when button one is clicked
}

-(void)buttonTwoFromCustomVCClicked:(id)sender {
    // Code for when button two is clicked
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 2017-09-02
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多