2岁的代码现在不起作用(WindowController alloc)

时间:2015-01-15 14:54:51

标签: objective-c macos cocoa

以下代码工作2年前

今天我尝试重建

AppDelegate.h

#import <Cocoa/Cocoa.h>
#include <stdio.h> 
#include <CoreServices/CoreServices.h>
#include <Carbon/Carbon.h>

@class EnterWindowController;


@interface AppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;

    EnterWindowController *vEnterWindowController;

}

@property (retain,nonatomic) EnterWindowController *vEnterWindowController;


@end

AppDelegate.m

#import "AppDelegate.h"
#include <stdio.h> 
#include <CoreServices/CoreServices.h>
#include <Carbon/Carbon.h>

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#import "EnterWindowController.h"

@implementation AppDelegate;
@synthesize vEnterWindowController;

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

    if(!vEnterWindowController)
    {


        vEnterWindowController=[[EnterWindowController alloc] init];

    }


    [vEnterWindowController showWindow:self];//point A
 }

与EnterWindowController相关的enterwidow不显示, 我将断点设置为A,发现vEnterWindowController为零, 它看起来像

 vEnterWindowController=[[EnterWindowController alloc] init]; 

不起作用并且总是返回nil。

您的评论欢迎

1 个答案:

答案 0 :(得分:1)

这个语法适用于我使用Xcode 6.1:

if (!vEnterWindowController)
{
    vEnterWindowController = [[EnterWindowController alloc] initWithWindowNibName:@"yourWindowNibName"];
}

[vEnterWindowController showWindow:self];

BTW你也可以在你的.h文件中使用

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <AppKit/AppKit.h>

@interface AppDelegate : NSObject <NSApplicationDelegate, , NSWindowDelegate> {

    @public

    EnterWindowController *vEnterWindowController;
}
@property (assign) IBOutlet NSWindow *window;

并且您不需要合成vEnterWindowController。您也不需要在.m文件中合成窗口。

NSWindowDelegate协议添加到AppDelegate也可以方便地接收有关主窗口的通知。