NSButton IBAction Crash无法识别的选择器

时间:2016-03-18 13:48:57

标签: objective-c macos cocoa-touch

遇到一个简单的问题而不太确定导致它的原因。将NSButton链接到ViewController xib。引用该属性,然后我将IBAction链接到视图控制器视图。每当我按下带有无法识别的选择器消息的按钮时,我都会崩溃。我知道我做错了,但在iOS上这是非常标准的。

以下是代码:

#import "AppDelegate.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    IPVLocationViewController *mainViewController = [[IPVLocationViewController alloc]initWithNibName:@"IPVLocationViewController" bundle:nil];
    self.window.contentView = mainViewController.view;
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

@end

#import "MainViewController.h"

@interface MainViewController ()
@property (weak) IBOutlet NSButton *mainButton;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
}

- (IBAction)mainClick:(id)sender {
    NSLog(@"THE BUTTON WAS CLICKED");
}


@end

difference between .then(…).catch(…) and .then(…, …)

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

在你的Nib中,看起来你的视图控制器实例是NSViewController类型而不是MainViewController。

因此,在Nib(或故事板)中选择视图控制器,并将其类型更改为MainViewController。

当然,如果这个视图控制器没有从笔尖或故事板加载,那么只需检查创建它的位置,并确保创建了正确类的实例。

答案 1 :(得分:0)

解决了它:

post中接受的答案帮助了我:

需要在AppDelegate中保存对视图控制器的引用。

@interface AppDelegate ()

@property (nonatomic, strong) MainViewController *mainViewController;

@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    self.mainViewController = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];
    self.window.contentView = self.mainViewController.view;

}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

@end