objective-c - 2 年前的代码现在不起作用(WindowController alloc)

标签 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为nil, 看起来像

 vEnterWindowController=[[EnterWindowController alloc] init]; 

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

欢迎您发表评论

最佳答案

此语法适用于使用 Xcode 6.1 的我:

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

[vEnterWindowController showWindow:self];

顺便说一句,您也可以在 .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 协议(protocol)添加到 AppDelegate 还可以方便地接收有关主窗口的通知。

关于objective-c - 2 年前的代码现在不起作用(WindowController alloc),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27966169/

相关文章:

c++ - 从 C++ 程序执行 Applescript

cocoa - 变换后CALayer与顶层重叠

objective-c - 什么是 NSTaggedDate?

objective-c - UIModalPresentationFullScreen 在 iPad Landscape 中无法正常工作?

swift - 快速设置屏幕上所有窗口的大小和位置

objective-c - 64 位模式下偶发 EXC_BAD_INSTRUCTION (SIGILL)

iphone - 了解应用程序委托(delegate)和 View Controller 如何相互关联

iphone - ccTouchMoved 反复触发声音 & ccTouchEnded 不起作用

objective-c - setCollectionViewLayout :animated causing debug error: Snapshotting a view that has not been rendered results in an empty snapshot

ios - 在 Objective-C 中从 PDF 中提取可编辑字段