如何用纯Swift 3替换.xib文件? [英] How can I replace my .xib file with pure Swift 3?

查看:77
本文介绍了如何用纯Swift 3替换.xib文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个仅在菜单栏中运行的macOS Cocoa应用程序。当我最初创建项目时,Xcode给我一个名为 MainMenu.xib 的文件。该 MainMenu.xib 文件似乎不包含与我的菜单栏应用程序相关的任何内容,因此我希望将其删除。

I am making a macOS Cocoa application which only runs in the menu bar. When I initially created the project, Xcode gave me a file called MainMenu.xib. This MainMenu.xib file does not appear to contain anything relevant to my menu bar application, so I wish to remove it.

可可入口点函数 NSApplicationMain ,从应用程序的主捆绑包加载主nib文件。我在其他地方读到可可通过查询我的 Info.plist 中的键 NSMainNibFile 找到了主要笔尖文件,该文件设置为字符串 MainMenu 。我删除了键 NSMainNibFile ,但是我的程序仍然表现相同。由此,我以为可可看到没有此键,因此跳过了它的笔尖/ xib加载步骤。

The Cocoa entry point, the function NSApplicationMain, "loads the main nib file from the application’s main bundle". I read elsewhere that Cocoa finds "the main nib file" by consulting the key NSMainNibFile in my Info.plist, which was set to the string MainMenu. I deleted the key NSMainNibFile, and my program still behaved the same. From this, I assumed that Cocoa saw the absence of this key, and so it skipped its nib/xib loading step.

由于我的 MainMenu。 xib 文件不再在任何地方被引用,我将其删除(这从我的 project.pbxproj 中删除​​了一些无辜的引用)。但是,删除 MainMenu.xib 之后,我的应用程序不再起作用。我的 AppDelegate 类上的 applicationDidFinishLaunching 方法未调用!

Since my MainMenu.xib file was no longer referenced from anywhere, I deleted it (which deleted some innocent-looking references from my project.pbxproj). However, after deleting MainMenu.xib, my application no longer works. The applicationDidFinishLaunching method on my AppDelegate class is not called!

这意味着两件事。首先,这意味着即使不存在 NSMainNibFile ,可可仍然可以神奇地找到我的 MainMenu.xib 文件(并且可可仍然找到了文件,如果我将其重命名为 Foo.xib )。更重要的是,这意味着我的 MainMenu.xib 仍然需要才能运行我的应用程序。这是完整的 MainMenu.xib

This means two things. First, this means Cocoa magically still finds my MainMenu.xib file even if NSMainNibFile is not present (and Cocoa still finds the file if I rename it to Foo.xib). More importantly, this means means that something in my MainMenu.xib is still required for my application to run. Here is the complete MainMenu.xib:

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11762"/>
    </dependencies>
    <objects>
        <customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
            <connections>
                <outlet property="delegate" destination="Voe-Tx-rLC" id="GzC-gU-4Uq"/>
            </connections>
        </customObject>
        <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
        <customObject id="-3" userLabel="Application" customClass="NSObject"/>
        <customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Foo" customModuleProvider="target"/>
    </objects>
</document> 

我认为问题在于我的 MainMenu.xib 引用了我的 AppDelegate 类,而Cocoa使用它来实例化 AppDelegate 类,然后调用 applicationDidFinishLaunching 在对象上。这令人困惑,因为我认为在 AppDelegate 类上的 @NSApplicationMain 注释已足够。

I think the problem is that my MainMenu.xib refers to my AppDelegate class, and Cocoa uses this in order to instantiate the AppDelegate class, and then call applicationDidFinishLaunching on the object. This is confusing, because I thought the @NSApplicationMain annotation on my AppDelegate class was sufficient.

因此,我有几个问题:


  1. 可可粉怎么样(即 NSApplicationMain )找到我的 MainMenu.xib 文件?可可的搜索过程是什么?我如何告诉Cocoa跳过此加载步骤?

  2. 我的 MainMenu.xib 文件在做什么? >也就是说,可可如何解释 MainMenu.xib 文件,其结果是什么?具体来说,此文件如何导致实例化我的 AppDelegate 类?

  3. 如何在纯编程方式中重新创建此逻辑? Swift 3,这样我就可以删除 MainMenu.xib 文件?我需要使用哪些API?

  1. How is Cocoa (i.e. NSApplicationMain) finding my MainMenu.xib file? What is Cocoa's search procedure? How do I tell Cocoa to skip this loading step?
  2. What is my MainMenu.xib file doing? That is, how does Cocoa interpret the MainMenu.xib file, and what does it do as a result? Specifically, how does this file result in my AppDelegate class getting instantiated?
  3. How do I recreate this logic purely programmatically in Swift 3, so that I can delete the MainMenu.xib file? What are the APIs I need to use?


推荐答案

问题#3的答案:


  • 创建一个Swift文件 main.swift

  • 将代码替换为 main.swift

import Cocoa

let appDelegate = AppDelegate()
NSApplication.shared().delegate = appDelegate
_ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)


  • 删除 AppDelegate 中的 @NSApplicationMain

  • 删除 Info.plist 中的键/值对 NSMainNibFile

  • 删除。 xib文件。

  • Delete @NSApplicationMain in AppDelegate.
  • Delete the key / value pair NSMainNibFile in Info.plist.
  • Delete the .xib file.
  • 这篇关于如何用纯Swift 3替换.xib文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆