先建立一個空白的iphone項目:
選圖中已經選中的表徵圖,然後點擊next,進入下一步:
為這個項目命名,然後next
選擇想要儲存的位置,然後建立就ok了
然後建立類來定義介面:
選擇圖中選中的類進行建立,點擊下一步
為類命名,進行下一步
選取路徑進行建立
建立好的項目為
在DemoViewController類中定義介面:(類中大多代碼為自動產生,在此唯寫重要代碼)
/*DemoViewController.h*/#import <UIKit/UIKit.h>@interface DemoViewController : UIViewController{ //定義一個Label,用來當容器 UILabel * helloLabel;}//nonatomic:提高效率,retain:setter方法對參數release舊值,返回新值@property(nonatomic,retain)UILabel* helloLabel;@end
/*DemoViewController.m*/#import "DemoViewController.h"@implementation DemoViewController@synthesize helloLabel;//先行編譯// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad{ //CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)設定Label顯示的位置 //CGRectMake(和左邊邊框的距離,和上邊邊框的距離,Label的長度,Label的高度) helloLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 300, 60)]; //設定Label中要顯示的內容 helloLabel.text = @"Hello World"; //對齊 局中(一行的中間) helloLabel.textAlignment = UITextAlignmentCenter; //設定字型顏色為紅色 helloLabel.textColor = [UIColor redColor]; //設定字型字型大小為20 helloLabel.font = [UIFont systemFontOfSize:20]; //把Label添加到View中 [self.view addSubview:helloLabel]; [super viewDidLoad];}
/*AppDelegate.h*/#import <UIKit/UIKit.h>@class DemoViewController;@interface AppDelegate : UIResponder <UIApplicationDelegate>{ //建立DemoViewController類的對象 DemoViewController* _iDemoViewController;}@property (nonatomic,retain)DemoViewController* iDemoViewController;@property (strong, nonatomic)UIWindow *window;@end
/*AppDelegate.m*/#import "AppDelegate.h"#import "DemoViewController.h"@implementation AppDelegate//備份變數名 通俗理解:作用就是讓編寫者學會運用self@synthesize window = _window;@synthesize iDemoViewController = _iDemoViewController;- (void)dealloc{ self.iDemoViewController = nil; [_window release]; [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; //建立出一個臨時的Demo DemoViewController *demo = [[DemoViewController alloc]init]; self.iDemoViewController = demo; [demo release];//釋放臨時Demo //添加到window中 [self.window addSubview:self.iDemoViewController.view]; [self.window makeKeyAndVisible]; return YES;}
然後運行項目就可以得到我們特別熟悉而又陌生的HelloWorld了