在xcode 8.3中创建仅限XIB的Cocoa项目 [英] Create a XIB only Cocoa Project in xcode 8.3

查看:369
本文介绍了在xcode 8.3中创建仅限XIB的Cocoa项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在XCode 8.3中创建基于XIB的应用程序,但是已经删除了启动没有Storyboard的项目的选项。以下是我设置项目的步骤:


  1. 创建一个新的Cocoa应用程序。

  2. 删除Main.Storyboard

  3. 删除ViewController.swift

  4. 向项目中添加一个新文件 - 一个Cocoa类 -
    的子类NSWindowController,同时创建XIB文件已检查 - 名为MainWindowController

  5. 在常规项目信息下删除对主界面的引用

  6. 在app delegate中:
    add var mainWindowController:NSWindowController? applicationDidFinishLaunching中的
    添加



    让mainWindowController = MainWindowController(windowNibname:MainWindowController)



    mainWindowController。 showWindow(self)



    self.mainWindowController = mainWindowController


  7. 在信息部分下的项目首选项中添加
    主nib文件基本名称MainWindowController


当我运行它时显示我的窗口然而,我得到了错误:



无法连接(窗口)插座(NSApplication)到(NSWindow):缺少setter或实例变量



如果我添加任何控件并连接它们,我会收到类似的错误,说明它们无法连接。



Phil

解决方案

Xcode 9及更高版本的更新



在Xcode 9.0及更高版本中(通过至少9.4 beta 1 ),您可以再次将Cocoa App项目模板配置为具有 M ainMenu.xib 而不是 Main.storyboard 。选择新项目的选项时,请确保未选中使用故事板选项:





以下是从头开始创建项目的步骤,该项目在一个XIB文件中包含主菜单栏,并且窗口位于单独的XIB文件中:


  1. 使用Cocoa Application模板创建一个新项目。


  2. 删除Main.storyboard和ViewController.swift(或ViewController.h和ViewController.m)。


  3. 使用用户界面部分中的主菜单模板创建新文件(文件>新建文件...)。将其命名为MainMenu(Xcode将自动添加.xib扩展名。)


  4. 在目标的常规设置选项卡中,更改主界面设置 MaintoMainMenu.xib。


  5. 在MainMenu.xib中,将NSObject添加到文档大纲中。将其自定义类设置为AppDelegate。演示:




  6. 在MainMenu.xib中,将文件所有者的委托插座(应该具有NSApplication类)连接到App Delegate对象。要执行此操作,请按住控制键并从文档大纲中的文件所有者拖动到应用程序代理。然后从弹出列表中选择delegate。


  7. 使用Cocoa Class模板创建一个新文件。将类名设置为MainWindowController,并将子类名称设置为NSWindowController。确保选中还为用户界面创建XIB文件。


  8. 在MainWindowController.swift中,为添加覆盖windowNibName 所有者

     类MainWindowController:NSWindowController {

    覆盖var windowNibName:String? {returnMainWindowController}
    覆盖var owner:AnyObject {return self}


  9. 在AppDelegate.swift中,添加代码以创建并显示 MainWindowController的窗口

      class AppDelegate:NSObject,NSApplicationDelegate {

    func applicationDidFinishLaunching(_ aNotification:Notification){
    mainWindowController = MainWindowController()
    mainWindowController!.showWindow(nil )
    }

    var mainWindowController:MainWindowController?


您现在可以在MainWindowController中创建其他对象.xib并将它们连接到MainWindowController.swift中的出口和操作。


I'm trying to create a XIB based application in XCode 8.3 but the option to Start a project without a Storyboard has been removed. Here are the steps I am taking to set up my project:

  1. Create a new Cocoa Application.
  2. Delete Main.Storyboard
  3. Delete ViewController.swift
  4. Add a new file to the project - a Cocoa Class - subclass of NSWindowController, Also create XIB file checked - named MainWindowController
  5. Under General project info- delete reference to Main for Main Interface
  6. in app delegate: add var mainWindowController: NSWindowController? in applicationDidFinishLaunching add

    let mainWindowController = MainWindowController(windowNibname: "MainWindowController")

    mainWindowController.showWindow(self)

    self.mainWindowController = mainWindowController

  7. in project preferences under the info section add Main nib file base name MainWindowController

This does display my window when I run it however, i get the error:

Failed to connect (window) outlet from (NSApplication) to (NSWindow): missing setter or instance variable

and if I add any controls and connect them i get a similar error stating they couldn't be connected.

Phil

解决方案

UPDATE for Xcode 9 and later

In Xcode 9.0 and later (through at least 9.4 beta 1), you can once again configure the Cocoa App project template to have a MainMenu.xib instead of a Main.storyboard. Just make sure the "Use Storyboards" option is not checked when choosing the options for your new project:

ORIGINAL

Your "Main Interface" setting should point to a storyboard or XIB that contains the application's main menu bar. You can create a XIB containing a main menu bar using either the "Application" file template or the "Main Menu" file template.

Here are the steps to create a project from scratch that has the main menu bar in one XIB file, and the window in a separate XIB file:

  1. Create a new project using the "Cocoa Application" template.

  2. Delete "Main.storyboard" and "ViewController.swift" (or "ViewController.h" and "ViewController.m").

  3. Create a new file (File > New File…) using the "Main Menu" template in the User Interface section. Name it "MainMenu" (Xcode will add the ".xib" extension automatically).

  4. In your target's General settings tab, change the Main Interface setting from "Main" to "MainMenu.xib".

  5. In "MainMenu.xib", add an NSObject to the document outline. Set its custom class to "AppDelegate". Demo:

  6. In "MainMenu.xib", connect the "delegate" outlet of "File's Owner" (which should have class "NSApplication") to the "App Delegate" object. To do this, hold the control key and drag from "File's Owner" to "App Delegate" in the document outline. Then pick "delegate" from the pop-up list.

  7. Create a new file using the "Cocoa Class" template. Set the class name to "MainWindowController" and set the subclass name to "NSWindowController". Make sure "Also create XIB file for user interface" is checked.

  8. In "MainWindowController.swift", add overrides for windowNibName and owner:

    class MainWindowController: NSWindowController {
    
        override var windowNibName: String? { return "MainWindowController" }
        override var owner: AnyObject { return self }
    

  9. In "AppDelegate.swift", add code to create and show the window of a MainWindowController:

    class AppDelegate: NSObject, NSApplicationDelegate {
    
        func applicationDidFinishLaunching(_ aNotification: Notification) {
            mainWindowController = MainWindowController()
            mainWindowController!.showWindow(nil)
        }
    
        var mainWindowController: MainWindowController?
    

You may now create additional objects in "MainWindowController.xib" and connect them to outlets and actions in "MainWindowController.swift".

这篇关于在xcode 8.3中创建仅限XIB的Cocoa项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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