简体   繁体   English

如何在 Mac OS X 10.15.2 (Catalina) 上使用 Obj-C、Xcode 11.3.1 在代码中重新定义应用程序窗口的大小/来源,覆盖 nib/xib 文件参数?

[英]How do I re-define size/origin of app window in code, overriding nib/xib file parameters, with Obj-C, Xcode 11.3.1 on Mac OS X 10.15.2 (Catalina)?

I'll try to keep it short.我会尽量保持简短。 I want to create a 3D FPS game, just for myself, that can run on multiple platforms, but I figured that to keep it simple, perhaps it is best to start off with something that is exclusively for macOS.我想为我自己创建一个可以在多个平台上运行的 3D FPS 游戏,但我认为为了简单起见,也许最好从 macOS 专用的东西开始。 I opted for Objective-C because我选择 Objective-C 是因为

(a) Window Application projects in Xcode can only be coded either in Obj-C or Swift (since we are dealing with Cocoa API) and (a) Xcode 中的 Window Application 项目只能用 Obj-C 或 Swift 编码(因为我们正在处理 Cocoa API)和

(b) Obj-C is closer to old-school then Swift. (b) Obj-C 比 Swift 更接近于老派。

But before I learn to draw/render 2D-shapes on the window's canvas by writing code, I have to learn to invoke an application window with its properties set to my liking.但在我学会通过编写代码在窗口的画布上绘制/渲染 2D 形状之前,我必须学会调用一个应用程序窗口,并根据自己的喜好设置其属性。 I've spent hours doing research and experimenting with chunks of code.我花了几个小时做研究和试验代码块。 This is what I've tried: I open with这就是我尝试过的:我打开

#import <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {

Then I go with ...那我跟...

1) 1)

        NSWindow *window = [[[NSApplication sharedApplication] windows] firstObject];
        NSRect frame = [window frame];
        frame.origin.x = 100;
        frame.origin.y = 200;
        frame.size.width = 100;
        frame.size.height = 500;
        [window setFrame: frame display: YES];

... and close with ... ...并关闭...


        NSApplicationMain(argc, argv); // runs the win display function.
    }

    return (0) ;
}

But no visible changes.但没有明显的变化。 Nothing really gets reset.什么都不会真正重置。 So instead of (1) I tried ...所以我尝试了而不是(1)...

2) 2)

        NSWindow *window = [[[NSApplication sharedApplication] windows] firstObject];
        NSPoint newOrigin;
        newOrigin.x = 400;
        newOrigin.y = 100;
        [window setFrameOrigin : newOrigin];

Still nothing.依然没有。 Then instead of (2) I tried:然后我尝试了而不是(2):

3) 3)

 NSWindowController* controller = [[NSWindowController alloc]
            initWithWindowNibName:@"MainMenu"];
        [controller showWindow:nil];

Great.伟大的。 Now it's spitting out something I don't understand, especially since I'm new to Obj-C:现在它吐出一些我不明白的东西,特别是因为我是 Obj-C 的新手:

2020-02-08 21:53:49.782197-0800 
        tryout_macApp2[14333:939233] [Nib Loading] Failed 
        to connect (delegate) outlet from 
        (NSWindowController) to (AppDelegate): missing 
        setter or instance variable

I remember dicing around with an ApplicationDelegate, with CGSizeMake(), etc., but it just made the experience really inundating and frustrating.我记得使用 ApplicationDelegate、CGSizeMake() 等等,但它只是让体验真的淹没和令人沮丧。 Nothing happened.什么都没有发生。 Then there are NSView, NSViewController, and other classes, which is really mindboggling and begs the question: why are there so many classes when all I want to do is override the preset origin of the window and the dimensions preset by the MainMenu.xib file?然后是 NSView、NSViewController 和其他类,这真是令人难以置信并引出了一个问题:为什么有这么多类,而我只想覆盖窗口的预设原点和 MainMenu.xib 文件预设的尺寸? (By the way, this project is derived from a Window Application project provided by Xcode.) (顺便说一下,这个项目是从 Xcode 提供的一个 Window Application 项目派生出来的。)

I really can't think of anything else to add to give you the entire picture of my predicament, so if you feel that something is missing, please chime in.我真的想不出还有什么可以补充给你我的困境的全貌,所以如果你觉得缺少什么,请插话。

[Edit:] Moving forward to phase 2 of my project here: How do I paint/draw/render a dot or color a pixel on the canvas of my window with only a few lines in Obj-C on Mac OS X using Xcode? [编辑:] 在此处进入我的项目的第 2 阶段: 如何使用 Xcode 在 Mac OS X 上的 Obj-C 中仅用几行在我的窗口画布上绘制/绘制/渲染一个点或着色一个像素? . .

The short answer is that main() is too early to be trying to do this.简短的回答是main()尝试这样做还为时过早。 Instead, implement -applicationDidFinishLaunching: on your app delegate class, and do it there.相反,在您的应用程序委托类上实现-applicationDidFinishLaunching:并在那里执行。 Leave main() as it was originally created by Xcode's template.保留main()因为它最初是由 Xcode 的模板创建的。

After that, I would say to obtain the window (if there's only going to be one main one), it's better to add an outlet to your app delegate and then, in the NIB, connect that outlet to the window.在那之后,我会说要获取窗口(如果只有一个主窗口),最好向您的应用程序委托添加一个插座,然后在 NIB 中将该插座连接到窗口。 Then, you can use that outlet whenever you want to refer to the window.然后,您可以在想要引用窗口时使用该出口。

Also, make sure that Visible at Launch is disabled for the window in the NIB.此外,请确保为 NIB 中的窗口禁用启动时可见。 That's so you configure it as you want before showing it.这样您就可以在显示之前根据需要对其进行配置。

For a more complex app, it's probably better to not put a window into the Main Menu NIB.对于更复杂的应用程序,最好不要在主菜单 NIB 中放置窗口。 Instead, make a separate NIB for the window.相反,为窗口创建一个单独的 NIB。 Then, load it using a window controller object and ask that for its window .然后,使用 window 控制器对象加载它并询问它的window

I love Objective-C but also feel your pain, it has this testy ability to frustrate you endlessly.我喜欢Objective-C,但也能感受到你的痛苦,它有一种让你无休止地挫败的暴躁能力。

I have not really developed a game but let me try and point you in the right direction.我还没有真正开发出游戏,但让我尝试为您指明正确的方向。 I think you need a UIViewController.我认为你需要一个 UIViewController。

Now each UIViewController has a built in UIView that sort of represents the visible portion of it.现在每个 UIViewController 都有一个内置的 UIView,它代表了它的可见部分。 You can use this or add a UIView and use that, whichever depends on your implementation.您可以使用它或添加一个 UIView 并使用它,这取决于您的实现。 For now I'd suggest add a separate UIView and use that rather.现在我建议添加一个单独的 UIView 并使用它。 Once you're comfortable you can then move the implementation to the UIViewController's view if you need to.一旦您感到舒适,您就可以根据需要将实现移动到 UIViewController 的视图中。

Anyhow, for now, create a UIView subclass, say MyGame or something, as for now all your code will end up there.无论如何,现在,创建一个 UIView 子类,比如 MyGame 之类的,至于现在你所有的代码都将在那里结束。

To do all of the above is not easy, especially if its the first time.要做到以上所有这些并不容易,尤其是第一次。 If you can follow some tutorial it will be great.如果您可以遵循一些教程,那就太好了。 Even if the tutorial just adds a button, you can use it and replace the button with your view.即使教程只是添加了一个按钮,您也可以使用它并用您的视图替换该按钮。

Anyhow, now that you've got that running and the view you've added shows up in green or some other neon colour just to verify that you can indeed change its properties, you're good to go.无论如何,既然您已经开始运行,并且您添加的视图以绿色或其他霓虹色显示,只是为了验证您确实可以更改其属性,那么您就可以开始了。

Now you start.现在你开始。 In MyGame, implement the -(void)drawRect:(CGRect)rect message, grab the context through UIGraphicsGetCurrentContext and start drawing lines and stuff on it, basically the stuff I understand you are interested in doing.在 MyGame 中,实现-(void)drawRect:(CGRect)rect消息,通过UIGraphicsGetCurrentContext获取上下文并开始在其上绘制线条和内容,基本上是我了解您感兴趣的内容。 You can also, through the same context, change the origin of what you are doing.您还可以通过相同的上下文,更改您正在做的事情的起源。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Xcode 在 Mac OS X 上的 Obj-C 中仅用几行在我的窗口画布上绘制/绘制/渲染一个点或着色一个像素? - How do I paint/draw/render a dot or color a pixel on the canvas of my window with only a few lines in Obj-C on Mac OS X using Xcode? 在Obj-C中,如何以编程方式在Mac OS X中设置文件的默认“打开方式”属性 - In Obj-C, how to programmatically set the default “open with” property of a file in Mac OS X 如何为OS X创建可可(Obj-C)动态库? - How do I create a Cocoa (Obj-C) Dynamic Library for OS X? 如何在Obj-C中使用回调定义Javascript函数? - How do I define a Javascript function with a callback in Obj-C? 如何在Mac OS 10.6+中使用obj-c切换到登录窗口 - How to switch to Login window using obj-c in Mac OS 10.6+ 如何在app delegate(obj-c)中添加UIAlertController - How do I add a UIAlertController in app delegate (obj-c) Obj-C,如何使用NSSearchPathForDirectoriesInDomains复制文件? - Obj-C, how do I copy a file with NSSearchPathForDirectoriesInDomains? 如何使用 Obj-C 轻松保存窗口大小和位置状态? - How can I easily save the Window size and position state using Obj-C? 如何找到有关Mac / Cocoa / Obj-C上计算机上运行的其他应用程序的信息? - How do I find information on other applications running on the computer on Mac/Cocoa/Obj-C? 澄清xcode obj-c代码 - Clarify xcode obj-c code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM