【问题标题】:Could not connect the action btnPressed to target of class NSViewController无法将动作 btnPressed 连接到 NSViewController 类的目标
【发布时间】:2014-12-04 23:02:16
【问题描述】:

我进行了搜索,但找不到与我类似的问题。

我有一个父视图 (AppDelegate) 和一个带有 NSButton 的子视图 (NSViewController)。

在我的父视图(AppDelegate)中,我使用 IB 在 MainMenu.xib 中添加了一个 CustomView 和 NSViewController 并使用以下代码将视图更改为 Child

AppDelegate.h -

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@end


AppDelegate.m -

#import "AppDelegate.h"
#import "Child.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSView *myCustomView;
@property (weak) IBOutlet NSViewController *myViewController;

@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    Child *childView = [[Child alloc] init];
    [[self.myViewController view] removeFromSuperview];
    self.myViewController = [childView initWithNibName:@"Child" bundle:nil];
    [self.myCustomView addSubview:[self.myViewController view]];
    [[self.myViewController view]setFrame:[self.myCustomView bounds]];

}

这里是 Child (NSViewController) ,我使用 IB 添加一个按钮并将其与我的 Child 链接。

Child.h -

#import <Cocoa/Cocoa.h>

@interface Child : NSViewController

@end


Child.m -

#import "Child.h"

@interface Child ()

@end

@implementation Child

- (IBAction)btnPressed:(id)sender {
}

@end

问题是没有编译错误,但是当我运行它时。它显示了

2014-10-09 22:51:43.952 Test[1439:303] Could not connect the action btnPressed: to target of class NSViewController

孩子可以捕捉到按钮按下事件。在我看来,Xcode 抱怨按钮操作未与父视图(AppDelegate)链接。

我怎样才能摆脱警告? Apple 是否允许带有此类警告的 OSX 应用程序?

【问题讨论】:

  • 您是否将按钮连接到您的操作方法?
  • 是的,我做到了。这就是为什么当我按下按钮时 btnPressed 被调用的原因。应用程序本身运行良好。我只是不知道如何摆脱这个消息。我想在这里上传项目,但我不知道如何。
  • 复制很简单。
  • 我尝试上传项目,但不知道如何上传。在 Xcode5 中重现它非常简单。步骤 1. 创建一个新的 Cocoa 项目。步骤 2. 在 MainMenu.xib 中添加自定义视图和视图控制器。第 3 步。使用基类“NSViewController”创建一个新的 Cocoa 类(“Child”)。不要忘记检查 xib 支持。步骤 4. 在 Child.xib 中创建一个 NSButton 并将操作与 Child.m 链接。第五步。将我的 applicationDidFinishLaunching() 复制到你的并运行。

标签: objective-c macos xcode5


【解决方案1】:

替换下面的行:-

Child *childView = [[Child alloc] init];
self.myViewController = [childView initWithNibName:@"Child" bundle:nil];

修改后的行:-

self.myViewController = [[childView alloc]initWithNibName:@"Child" bundle:nil];

注意:- 请确保您提到的 xib 名称正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 2016-03-23
    相关资源
    最近更新 更多