▼iPhoneプログラミングメモ▼
HelloWorld
「Hello, World!」という文字列を表示するプログラムを作成する。
ソースコードの記述
AppDelegate .h #import <UIKit/UIKit.h> //AppDelegateの宣言 @interface AppDelegate : UIResponder <UIApplicationDelegate> { UIWindow* _window; } //プロパティの宣言 @property (nonatomic,retain)IBOutlet UIWindow* window; @end
AppDelegate .m #import "AppDelegate.h" #import "HelloWorld.h" //AppDelegateの実装 @implementation AppDelegate //プロパティの実装 @synthesize window=_window; //アプリ起動時に呼ばれる - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { //ウィンドウの生成と追加 _window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; _window.backgroundColor=[UIColor whiteColor]; [_window makeKeyAndVisible]; //ビューの生成と追加 CGRect bounds=[[UIScreen mainScreen] bounds]; bounds.origin.y+=20; bounds.size.height-=20; UIView* view=[[[HelloWorld alloc] initWithFrame:bounds] autorelease]; [_window addSubview:view]; return YES; } //メモリ解放 - (void)dealloc { [_window release]; [super dealloc]; } @end
HelloWorld.h #import <UIKit/UIKit.h> //HelloWorldの宣言 @interface HelloWorld : UIView { } @end
HelloWorld.m #import "HelloWorld.h" //HelloViewの実装 @implementation HelloWorld //初期化 - (id)initWithFrame:(CGRect)frame { self=[super initWithFrame:frame]; if (self) { //背景色の指定 self.backgroundColor=[UIColor whiteColor]; } return self; } //メモリ解放 - (void)dealloc { [super dealloc]; } //描画 - (void)drawRect:(CGRect)rect { UIFont* font=[UIFont systemFontOfSize:24]; [@"Hello, World!" drawAtPoint:CGPointMake(0,0) withFont:font]; } @end