我做了搜索,找不到与我类似的问题。
我有一个父视图(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.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应用程序出现此类警告?
答案 0 :(得分:0)
替换以下行: -
Child *childView = [[Child alloc] init];
self.myViewController = [childView initWithNibName:@"Child" bundle:nil];
修改后的行: -
self.myViewController = [[childView alloc]initWithNibName:@"Child" bundle:nil];
注意: - 请确保您提到的xib名称正确无误。