這次要謝謝盈宏哥指導,不然操作Xcode開發OSX對我來說有點難XD
首先新增一個專案,Application/Cocoa Application
Product Name:Hello,語言要選擇Swift
選擇儲存位址
專案開啟完的起始畫面如下
AppDelegate.swift是點擊UI的任何原件時會觸發的控制器
MainMenu.xib:則是UI畫面
右下角會有些UI元件可以使用,將元件拖曳到視窗即可
接著移到右上角,選擇Show the Assitant editor
將UI放置左邊的視窗,控制器的程式碼放置右方
將你想要連接的元件按下control+按著控制板拖曳到程式碼就可以連接了
拖曳完就可以開始撰寫程式碼
AppDelegate.swift:
// // AppDelegate.swift // Hello // // Created by CYFang on 6/14/14. // Copyright (c) 2014 CYFang. All rights reserved. // import Cocoa class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet var window: NSWindow @IBOutlet var myTextField : NSTextField @IBOutlet var myText : NSTextField func applicationDidFinishLaunching(aNotification: NSNotification?) { // Insert code here to initialize your application } func applicationWillTerminate(aNotification: NSNotification?) { // Insert code here to tear down your application } @IBAction func myClick(sender:AnyObject){ let title = myTextField.selectedCell().title() myText.setTitleWithMnemonic("Hello \(title)") } }
執行結果:
可能是平常的開發環境都是Eclipse or Visual Studio,所以不太習慣Xcode的開發方式
習慣後覺得蠻直覺的,但螢幕太小Orz..
參考資料:
https://developer.apple.com/library/mac/navigation/
https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSView_Class/Reference/NSView.html
https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSTextField_Class/Reference/Reference.html
http://stackoverflow.com/questions/24005678/what-is-the-equivalent-of-an-objective-c-id-in-swift