顯示具有 xcode 標籤的文章。 顯示所有文章
顯示具有 xcode 標籤的文章。 顯示所有文章

2016-08-21

在 IDE 編輯器前面出現的 Entry Types

前文: 簡碼到用時方恨少

從 Xcode 的 Code Autocomplete 出現的提示畫面前都有個小圖示的符號,像這樣
C 你應該曉得是 Class,那麼 # 字號呢?你用過 NSDictionaryOfVariableBindings 應該知道是 Macro,但是你有沒有想過去那裡查詢嗎?

這裡是 iOS、OS X 的代碼


以及 Swift 的代碼


希望對你有所幫助

.

2014-11-14

Xcode 6 在模擬器 iPhone 5 + iOS 7 的版面上下黑邊

如果您在 Xcode 6 發現了模擬器以 iPhone 5 執行 iOS 7 時,是以 320x480 的版面而非 320x568 時,應該如何排解?
Xcode 6 執行選擇 iPhone 5/5s + iOS 7.1

iPhone 5 + iOS 7 不應該上下有黑邊
照理說,iPhone 5 應該是 4 吋 Retina 的解析度 320x568 不應該成為 320x480 而讓畫面留下上下的黑邊。

原因是這樣:

原來是在 4 吋 Retina 的設備,少了 Default-568h@2x.png 的啟始畫面。

註:在 iOS 7.1 以前需要的啟始畫面檔案
  • Default.png - 320 x 480 pixels (standard resolution iPhone)
  • Default@2x.png - 640 x 960 pixels (high resolution iPhone 4)
  • Default-568h@2x.png - 640 x 1136 pixels (iPhone 5)



所以,如果您的 App 專案有這樣的設定時:

解決的方式,除了可以加入一個 640 x 1136 的 Default-568h@2x.png 檔案之外,可以在 Project 的 TARGETS 設定內,從 General 頁面內的 App Icons and Launch Images 將 Launch Images Source,按下「Use Asset Catalog」


您再執行看看,結果就不會在 iPhone 5/5s + iOS 7.1 的畫面上下出現黑邊了。

.

2014-07-09

Mac App 的 NSTextField Number Formatter - 處理小數點位數

前陣子為了解決 MacBook 畫面頂端功能表,因為程式的不同而有項目多寡,導致常駐程式被隱藏起來,像這樣:
Finder,有些常駐程式看不到


Xcode,常駐程式看到更少



於是做了一個 Mac App 叫做 M。
常駐程式全部都出來了吧

畫面很簡單:只有留一個 M 的程式名
後來加了「度分秒」與「度數」轉換,在查閱 Google 地圖時可以換算。
但是習慣小數點後面 4 位數,於是就找了一下:



按照這 4 個步驟,Xcode 夠可愛吧,簡單!

於是,小數點位數也正確了。


如果你有需要的朋友,可以免費下載使用

.

2014-03-01

Xcode 使用 /* ... */ 的快速鍵

大部份的朋友都知道 ⌘ + / 可以加上單行註解,或是將程式碼選取的那幾行註解。
可是,Xcode 是加上 // 的註解,如果你需要的是區塊註解「/* ... */」的話,那麼要用哪個快速鍵呢?

善用 Mac 的 Automator 吧!


做法可以參考 WWDC 2012 Session 402 - Working Efficiently with Xcode,在 6 分鐘處。


使用 Automator 加入 Service 服務,指令如下:


awk 'BEGIN{print "/*"}{print $0}END{print "*/"}'



然後在你的 Xcode 內就可以在選取的區塊,按下右鍵(或是 Ctrl+Click) 在 Service 選取你設定的 Service 名稱。




WWDC 2012 Session 402 - Working Efficiently with Xcode 

https://developer.apple.com/videos/wwdc/2012/


圖解可以參考 行動開發學院

.

2013-09-11

簡碼到用時方恨少

需要時可以查閱。


在這裡,看到了吧!


編輯器輸入點移到關鍵字後面,按下 ESC 按鍵,就可以看到。

.



2012-11-14

新版本 Xcode 的專案如何在舊版本 Xcode 開啟

我在寫這個文章的時間點,剛好 Mac OS X 普遍有 10.6,10.7,10.8 三個版本。
同時,Xcode 可以開發 iOS 5 以上,又有 StoryBoard 的,至少是 4.2(4C199)。
4.2(4C199) for Mac OS X 10.6.x

另外,Xcode 目前最新的版本是 4.5.2 (4G2008a),新版的 Xcode 除了開發 iOS 6 需要之外,在設計時的效率可以幫助不少,所以可以更新的建議就直接更新。
4.5.2 (4G2008a) for Mac OS X 10.8.x
但是,如果你的 Mac 是 OS X 10.6.x,你的 Xcode 頂多只可以升級到的盡頭是 Xcode 4.2(4C199) 這個版本,也就是只有開發 iOS 5 的 App。

這兩年的 Xcode 變化很大,尤其是加入了 StoryBoard,剛好我以前的教材範例多用 XIB,為了轉換到 StoryBoard,已經改過一次了。

又遇到了客戶的專案以前用 4.2 寫的,雖然我的 iMac 上面還留著 Mac OS X 10.6,Xcode 還有兩個版本:Xcode 3.2.6 for iOS 4.3、Xcode 4.2(4C199) for iOS 5。
身邊的 MacBook Air 是 Mac OS X 10.8 & Xcode 4.5.2 (4G2008a)。

我剛好遇到過這個困擾:常常在 MacBook Air 做完,拿回到 iMac 上打不開。

其實,只要在新版 Xcode 選 MainStoryBoard,在 Interface Builder Document 屬性設定三個地方就可以解決這個困擾:
  1. Deployment 佈署設為 iOS 5.0
  2. Development 開發環境設為 Xcode 4.2
  3. Autolayout 取消勾選。
然後再將整個 Project 拿到舊版環境就可以開啟了。

在新版 Xcode 設定三個地方
只是你要知道,佈署與開發環境退回去舊版,相對的是不相容 iOS 6 的設備了。

以上希望對您有所幫助。

.

2010-10-16

網路傳輸時 UIActivityIndicatorView 的動作

這已經是習慣了,大家都習慣要有這個符號出現。
當你的程式有使用到網路傳輸,通常就需要放一個 UIActivityIndicatorView 在畫面上,讓使用者知道目前正在傳輸中,以免讓人覺得沒有回應,像是當機一樣。
可是,為何你照著書本範例加到自己的程式卻不會動呢?

原因是:UIKit 的 main thread 一次只有做一件事,如果你在 ViewController 傳輸檔案,那麼其他的動作會等傳輸完之後再接下去做,所以你要另外再生出一個 thread 去處理 UIActivityIndicatorView 的動作。

使用 Objective-C 來處理 thread 是很簡單的,比 Java 還容易,在後面的說明裡會看到。


首先,在 .h 檔的 @interface 內加上

UIActivityIndicatorView *activityIndicator;


以及

@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;


另外,再加上兩個 method

-(void)actIndicatorBegin;
-(void)actIndicatorEnd;


開啓 .m 檔,在 @implementation 的下面加上

@synthesize activityIndicator;


在 (void)loadView 內把你的 UIActivityIndicatorView 加上去你要的 View 上面,我這裡加在 UIToolbar 工具列裡面的一個 UIBarButtonItem 按鈕

activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
[activityIndicator setCenter:CGPointMake(12, 12)];
[activityIndicator setHidesWhenStopped:YES];
[activityIndicator setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhite];
[activityIndicator startAnimating];
UIBarButtonItem *activityItem = [[[UIBarButtonItem alloc] initWithCustomView: activityIndicator] autorelease];


再把這個 UIBarButtonItem 加到 UIToolbar 內

NSArray *items = [NSArray arrayWithObjects:activityItem, nil];
toolbarTop.items = items;


在處理網路傳輸之前先執行這個 thread

[NSThread detachNewThreadSelector: @selector(actIndicatorBegin) toTarget:self withObject:nil];

//需要時間下載檔案的程式寫在這裡

url = [NSURL URLWithString:@"http://網址/圖片檔.jpg"];
img =[UIImage imageWithData:[NSData dataWithContentsOfURL:url]];


網路傳輸完之後再執行這個 thread

[NSThread detachNewThreadSelector: @selector(actIndicatorEnd) toTarget:self withObject:nil];


再加上這兩個 method 開關旋轉狀態
- (void) actIndicatorBegin {
[activityIndicator startAnimating];
}

-(void) actIndicatorEnd {
[activityIndicator stopAnimating];
}


就這麼簡單,做看看吧!

.

2010-02-24

如何利用有限的時間學習 Xcode 與 iPhone 軟體開發

如果你也與我一樣(白天上班有公司的工作,下班後晚上有家庭、小孩課業,肯定也是沒有很多時間的朋友),自己的時間已經很少了,但是要怎麼著手學習 Xcode 與 iPhone 軟體開發?
你看到我前幾篇的文章提到,有那麼多的開發手冊要看,光是看目錄就暈倒了,還要看到何時?

我把我之前的經驗在這裡與各位分享,大家參考看看。

除了讀開發手冊之外,可以快速進入開發狀況的是史丹佛大學的 iPhone Application Development 課程,目前的「Winter 2010」版已經第三年了,之前兩年訂閱後都是在 Podcast 內,今年的訂閱後會歸類在 iTunes U 裡,而且還有字幕,對於聽力不是很強的朋友,是一個很好的機會。

訂閱後你就可以下載每個課程,然後同步在 iPhone/iPod touch 裡,隨時隨地可以利用時間學習。

訂閱方式如下:
從 iTunes 左側的 STORE 點選 iTunes Store

進入 iTunes U,按右邊選單的 Universities & Colleges

這時候你會看到很多學校,用英文字母分類,我們找到 S 的 Stanford
(如果你對其他學校的課程有興趣,也可以多看看)

進入之後,你可以從 CATEGORIES 的 Science and Technology 找到,或是直接從 WHAT'S NEW 就可以看到 iPhone Application Development (Winter 2010)

在 iPhone Application Development 的介紹頁面,按下 SUBSCRIBE 訂閱之後,就可以在 iTunes 的 iTunes U 資料庫內看到課程列表

另外,最快的方式(省略上述步驟)是直接從 iTunes 「功能表\進階\訂閱 Podcast..」. 輸入以下的 URL
http://deimos3.apple.com/WebObjects/Core.woa/Feed/itunes.stanford.edu.3124430053.03124430055

這樣也可以直接訂閱,然後就可以下載你要看的科目了。


*2010/11/06:
如果對物件導向不熟的,可以參考一下這篇


.

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-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)
.