本章將建立一個最簡單的iphone範例程式(xcode 4.2),如同所有的入門範例ㄧ樣
將使用Hello World
1.如下圖,選取第一個選項,"Create a new xcode project"
2.此project的template使用sing view application來建立
3.賦予此project一個有意義的名字,在本章中,先不使用storyboard,並且使用GC(Garbage collection),所以注意到該圖的勾選項
4.點選左側project,viewController_iphone,在開啓右側的object inspector,將Button和TextField拖曳到viewController_iphone.xib(a.)(b.),並且修改這兩個component的title & text
5.右鍵點選viewControllor_iphone上的textField元件,會出現一個小視窗,右鍵點選"new reference outlet"的黑色小圈圈並且拖曳到viewControllor.h,會產生
@property (retain, nonatomic) IBOutlet UITextField *textField0;
並且在viewControllor.m會產生
@synthesize textField0;
此程式碼將產生一個和xib上的textField對應的property.
6.右鍵點選viewControllor_iphone.xib上的Button元件,會出現一個小視窗,右鍵點選“Touch up inside"的黑色小圈圈並且拖曳到viewControllor.h,會產生一個白色視窗,在connection內選擇action,並且在name的欄位內填入此action的名字"pressedButton",接著就產生了相對應的程式碼
- (IBAction)pressedButton:(id)sender;
7. 接著在viewControllor.h上增加一個變數
@interface ViewController : UIViewController
{
UITextField *textField0_;
}
並且在viewControllor.m加入
@synthesize textField0=textField0_;
完成此property對內存變數的存取對應
8.在viewcontrollor.m完成pressedButton的implement部分
- (IBAction)pressedButton:(id)sender
{
textField0_.text=@"let's go";
}