2009-11-07

Xcode 常用技巧與按鍵

每個文字編輯軟體都有一些內定的技巧與按鍵,讓我們在編寫程式碼的時候很方便又有效率,列出來參考:

原文出處
http://iphonedevelopertips.com/xcode/xcode-tips-and-tricks-tips-1-to-10.html

Tip #1 – Split Editor View Vertically
If you like to have multiple code windows open at once, the Split Editor option is your friend (see the little square grid icon shown below).

By default, the windows are split horizontally. If you prefer to view your code side-by-side (nice for comparing), here’s how to tell the split to be vertical
Hold down Option key when clicking the Split Window icon

Tip #2 – Comment Out a Block of Code
You can quickly comment out a block of code as follows:
Select one or more lines of code to comment
Command-/
To uncomment a block of code, repeat the steps above.

Tip #3 – Toggle Between .h and .m Files (aka Switch to Counterpart)
Within your current code window, you can swap between the .h and .m file as follows:
Command-Option Up-Arrow

Tip #4 – Map Keys to Actions (Key Bindings)
The Switch to Counterpart tip above is a real time saver. However, I often find that if I map logical key-strokes to actions I’m much more likely to get into the habit of using them. For example, I mapped the Switch to Counterpart to Option-S, as in Swap or Switch.
Here’s how to set key bindings in Xcode
From the Xcode menu choose Prefereces
Select Key Bindings
Click on one of the Actions in the list
Tap on the Keys column on the right
Enter the keystroke you want to map to the action
Click Ok to save your changes

Tip #5 – Jump to API Documentation
Showing relevant API documentation for anything within the SDK(s) is as simple as:
Option Double-Click on relevant code
For example, in the screenshot below, after Option double-clicking on UIToolbar, a popup window shows a summary of the class information.

Tip #6 – Traverse File History
As you open and edit various files, Xcode keeps a history list of your actions, not unlike when using a web-browser. You can move through the list using the directional arrows as shown below:

The keystoke equivalent for the above is:
Option-Command Left-Arrow goto to previous file
Option-Command Right-Arrow goto to next file

Tip #7 – Set a Bookmark
I can’t imagine coding without having the option to set a bookmark as a placeholder for what I’m working on. I do this regularly when I need to segue to another file to find a snippet or otherwise poke around outside the current file I’m working on.
Setting a bookmark is as simple as:
Control-D
Enter a name for the bookmark

Tip #8 – Jump to Bookmark
There are two options to jump to a bookmark. First, you can select the bookmark icon in the upper right corner of the Editor window.

You can bring up the same menu as shown in the above figure using this keystoke:
Control-4

Tip #9 – Indent / Un-indent Code
You can indent a line of code or a selected block of code as follows:
Command-[ move code left
Command-] move code right
The above works regardless of where you are in a line of code, in other words, you don’t have to be at the beginning of the line.

Tip #10 – Zoom Editor
You can toggle between Detail view and Editor view by clicking the Editor button as shown here:

The keystroke equivalent of the above is:
Shift-Command-E


2009-10-21

為何 Android 很難成功?

有朋友看到 HTC Hero 之後,很高興地說要去買一支來用,我都力勸不要浪費那些錢,寧可多存一點錢追加買 iPhone。我說,可以等,等看看,最後 Android OS 還是會一團亂!

轉貼幾則徵候,分析很清楚:

Android即將可能面臨的分裂問題

Google要自己做手機?軟體還一堆事該做咧.....

.

2009-10-08

用 Flash 開發 iPhone 軟體

在 Adobe Labs 的網站有提到 Flash CS5,將於 2009 年底以前上市。可想而知的是,Flash CS5 上市之後就會有很多小遊戲出現在 AppStore,而且會越來越多,最讓大家期待的「可不可以在 Windows 開發 iPhone 軟體」將會實現。

根據 Adobe 的資深產品經理 Aditya Bansod 提到,Flash CS5 編譯成的軟體,是可以在 iPhone armv6 CPU 執行的 native 原生軟體,而且還可以使用 SQLite。至於進一步有關描述檔(provisioning profiles)的取得,也可以看 FAQ

以目前狀況來看,要把軟體上架到 AppStore,只有用 Objective-C,雖然不難,但或許對一部份的人會有障礙;而對手呢?在這個月上線的 Microsoft Marketplace,也要用 c# 來開發,也是有一點門檻。

Microsoft Marketplace 雖然比較晚上來,雖然使用 Windows 的人口很多,但對於軟體市集能不能成功還很難講,因為這個架構不是一年兩年可以學得來。

Apple 也會防止 Microsoft Marketplace 的壯大,為何 Xcode 不要發行 Windows 版本?或許是 Apple 要藉 Adobe Flash 擁有廣大開發人口加入 iPhone 軟體的開發,乘勝追擊,讓開發人員直接寫軟體送進 AppStore,這樣一來,對於 Microsoft Marketplace 投入的人就不會那麼多了。

Flash 是一套很視覺化的物件設計環境,如果可以拿來開發 iPhone 原生軟體,真的很讓人期待!

是不是這樣?我們再拭目以待!


*** 2011.4.24
很久沒有注意這方面的消息,有朋友留言給我,於是我再到 Adobe 的網站看看目前狀況。
有興趣的朋友可以到這裡 Packager for iPhone 保持注意。
.

2009-08-16

給 Delphi 設計師快速入門 iPhone 程式設計

我最近在想,要如何幫助 Delphi 或是 VB 的設計師們可以快速入門 iPhone 程式開發。
Delphi 之所以方便使用,是提供了一系列方便使用的 VCL 元件,讓設計師可以快速地把視窗軟體實作出來。
所以,這次我就把 IB (Interface Builder) 與 Xcode 之間很類似 VCL 的拖曳觀念,來寫一篇教學文件給大家參考,希望可以有拋磚引玉的作用。
我在這裡的範例是做一個調色盤,有三個分別是紅綠藍的 Slider Bar,藉由 Slider Bar 的調整,讓整個背景隨時呈現色彩。


首先,開啟 Xcode 新增一個 Window-based Application
取名 palette

產生新的 Project 如下:

找到 MainWindow.xib 開啓,會交由 Interface Builder (IB)

在 IB 會看到兩個視窗,一個是 MainWindow.xib (像是 Delphi 的 View Forms/Units)。
(如果畫面不一樣,改變工具列上面的 View Mode)

另一個是 Window 的 View。(像是 Delphi 的 Form)

在 IB 按下 +SHIFT+L 開啓 Library (像是 Delphi 上面的 Components Palette)

將 Slider 拖曳到 Window 的 View 上面排列、調整位置、大小 (會 Delphi 這個就不難)

在 IB 按下 +SHIFT+I 開啓 Inspector (像是 Delphi 的 Object Inspector)。
設定 Slider 的『屬性』(記得 Window 點選到 Slider,跟 Delphi 設定屬性原理一樣)
Value Minimum: 0,Maximum: 255

再拖曳兩個 Label 進來 Window 的 View 上面排列,只要對該 Label 點兩下,就可以直接修改內容,分別給 Red Value 與 0

把這三個選取後,複製貼上(就像在 Delphi 的 Form 上面作業一樣),一共產生三組,分別要處理 RGB 用。

您也可變更 Label 的顏色『屬性』

這樣會比較好辨識
到這裡都很簡單吧!
把 Object 拉到 Window 的 View 上面,就像是在 Delphi 把 VCL 元件拉到 Form 上面一樣簡單。



接下來我們把元件的關係連結起來。

從 Library 把 NSObject(金黃色正方體) 拉到 MainWindow.xib 上面
看到這樣:
然後按 +4 把 Inspector 切換到 Identidy

這裡我們要設定 Class Identity, Class Action, Class Outlets 等三個部份

Class Identity 類別識別,目前是 NSObject,我們設定名稱為 Controller
Class Action 是指元件的動作,相當於 Delphi 的事件
Class Outlets 是哪些元件會在程式內用到的、會與畫面互動的,可以在這裡指定

再看一次我們的畫面,我命名如下:
sliderR, sliderG, sliderB 會觸發事件,我們要在程式內安排程式碼。
labelR, labelG, labelB 會因為事件發生,在程式碼內改變他們的屬性。

所以把這6個加到 Class Outlets 內。


確認 MainWindow.xib 點選到的是剛剛加進來的 Object (Type: NSObject)。
Class Identity 的 Class 命名為 Controller。
Class Action 分別加上 (在框框下面有個 + 號)
  • sliderChangedR: (Type: id)
  • sliderChangedG: (Type: id)
  • sliderChangedB: (Type: id)
Class Outlets 分別加上
  • sliderR (Type: UISlider)
  • sliderG (Type: UISlider)
  • sliderB (Type: UISlider)
  • labelR (Type: UILabel)
  • labelG (Type: UILabel)
  • labelB (Type: UILabel)

接著我們把 IB 建立的產生成 Class 檔案,選取功能表 File\Write Class Files...
接著的視窗內,Save As: 會依照剛剛 Class Identity 的 Class: Controller 命名
把檔案儲存在 Classes 的檔案夾內(我習慣把 Class 都放到 Classes 檔案夾內)
按下 Save 後,會出現詢問視窗,是否要把 Controller.m 與 Controller. h 加到 palette 專案裡,記得勾選 palette 後按下 Add
回到 Xcode 會多出這兩個檔案 Controller.m 與 Controller.h

Controller.h 的內容


Controller.m 的內容


開啓 Controller.h 將 @interface Controller: 後面加上 NSObject
這裡可以看到 6 個 IBOutlet 與 3 個 IBAction 的定義

再來開啓 Controller.m 來安排事件程式:
依照 sliderR, sliderG, sliderB 的變化而改變 labelR, labelG, labelG 的值。

這時候興奮地按下 Build and go,出現 iPhone 的畫面了!
可是拉動 Slider 並不會看到數值改變啊!

那是因為我們在 IB 還沒有把 Connections 建立起來。
所以回到 IB 點選 MainWindows.xib 的 Controller,然後按 +2 把 Inspector 切換到 Connections Inspector:
這裡區分三個部份,我們要用前面兩個:Outlets 與 Received Actions。
看到 Outlets 內的 labelB 右邊的圈圈吧!將游標移到上面,會出現一個 + 號,然後按住滑鼠左鍵,移動到 Window 上面的 labelB 然後放開滑鼠左鍵。在移動滑鼠的時候,會看到一條藍色的連結線,讓設計者很清楚哪一個要連結。


Connections Inspector 的 Outlets 內 labelB 會有連結 Label (0) 的顯示

依此類推,將 labelG, labelR, sliderR, sliderG, sliderB 也都連結起來

然後換 Received Actions 內的三個。

一樣從 sliderChangedB 右邊的圈圈開始,拉到 Window 的藍色 Slider 上面,然後放開滑鼠左鍵

在放開滑鼠左鍵後會看到這個視窗,點選 Value Changed
依此類推,可以看到 Received Actions 顯示

記得把 MainWindow.xib 存檔後,回到 Xcode
Build and go 就可以看到結果了!

最後階段要改變 Window 背景顏色

開啓 Controller.h 加上 #import "paletteAppDeledate.h"

開啓 Controller.m 在 @implementation 上面加上 @class paletteAppDelegate;

因為 window 的這個元件是在 paletteAppDelegate 內,我們要從 Controller 去使用 paletteAppDelegate 內的元件。
(有點像是 Delphi 要把被使用的 Unit 加到 uses 內)

接著在這三個事件內加上這兩行

最後的 Controller.m 程式

執行看看,成功了吧,恭喜!

*最後的 Controller.m 內,三個事件 sliderChangedR, sliderChangedG, sliderChangedB 重複的那兩行程式,可以另外做一個 Method 處理,就留給各位練習看看。

範例程式下載1(github)

範例程式下載2(bitbucket)
.