2009年9月30日星期三

xcode下開發1

因公司要做iPhone版本的一個產品,所以需要使用xcode這個東西,經過兩天的折騰,終於寫了個HelloWorld出來,對object c也有了初步的認識

這裡使用的是xcode3.0版本,參考 (Jailbreak Goddess) Erica Sadum 提供了大量的代碼,我是比較愚鈍的,進展很慢:(,不過終於搞好了以一個hello world
為了閱讀方便,作者把所有的代碼寫到一起了
---------------main.c------------------------
#import

@class UIImageView;

@interface HelloController : UIViewController
{
UIImageView *contentView;
}
@end

@implementation HelloController
- (id)init
{
if (self = [super init])
{
self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
// place any further initialization here
}
return self;
}

/**
*建立起主要的應用程序VIEW
**/
- (void)loadView
{
// Load an application image and set it as the primary view
contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[contentView setImage:[UIImage imageNamed: @"helloworld.png" ]];

// Provide support for auto-rotation and resizing
contentView.autoresizesSubviews = YES;
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

// Assign the view to the view controller
self.view = contentView;
[contentView release]; // reduce retain count by one

}

// Allow the view to respond to iPhone Orientation changes
/**
* 為自動旋轉功能准備幾個標志
**/
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

-(void) dealloc
{
// add any further clean-up here
[contentView release];
[super dealloc];
}
@end

/**
* UIApplicationMain()呼叫跑到這裡了
**/
@interface SampleAppDelegate : NSObject {
}
@end

@implementation SampleAppDelegate

// On launch, create a basic window
/**
* applicationDidFinishLaunching: 方法
*標准的mainScreen繪制一個新視窗, 它會建立一個導覽控制程序,並知道給一個新的HelloController(UIViewControler子類別)來當作他自己的根源試圖控制程序(Root View Controller)
**/
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[HelloController alloc] init]];
[window addSubview:nav.view];
[window makeKeyAndVisible];
}

- (void)applicationWillTerminate:(UIApplication *)application {
// handle any final state matters here
}

- (void)dealloc {
[super dealloc];
}

@end


/**
main函數是xcode自動產生的,而且還保持未經過人變動的原貌
他呼叫 UIApplicationMain(),並將主要的應用程序的委派方法名稱,這裡是 @"SampleAppDelegate"傳給他!
*/

int main(int argc, char *argv[])
{
/**
*這裡應該是內存分配
**/
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
printf("hellowrold\n");
int retVal = UIApplicationMain(argc, argv, nil, @"SampleAppDelegate");
[pool release];
return retVal;
}

---------------------------------------------
我創建了一個新的PROJECT,然後把上面的代碼copy過去,運行是報錯.屢試不爽.
後來按照書中的要求一步步來,就沒有出錯了,環境不熟悉阿:
記錄一下:
1 刪除classes 文件夾
2 刪除.xib文件
3 在info.list,我的版本上是helloworld-info.list文件中的"NSMainNibFile"的KEY-VALUE 這一行
4 加入image文件夾,這裡是用ADD NEW GROUP
5 拖文件到這個文件夾中(xcode環境中)
6 用上面的main.m覆蓋原來的內容
7 COMMONAD + ENTER 運行模擬器
如果沒有問題的話就OK了

這裡有幾個小技巧:
COMMAND + <
COMMAND + > 模仿IPHONE橫位

COMMAND + SHIHF + R 可以看到後台運行的情況, 我原來一直不知道printf打LOG信息從那裡看,這下子就知道了:)

INSTRUMENTS的執行方式: instruments是APPLE基於SUN的DTRACE搞的,一直很向往之,終於見識了,設定方法:
XCODE環境 -->RUN --> START WITH PERFORMANCE TOOL ,有幾個方式, 我猜OBJECT ALLOCATION 和 CPU SAMPLER應該是比較常用的. 估計以後查內存洩露的時候可以用到.




没有评论:

发表评论