isTKS™ ≫ objective-C ≫ XcodeでiPhoneアプリ開発。objective-CでHelloWorldを出力。
XcodeでiPhoneアプリ開発。objective-CでHelloWorldを出力。
XcodeでiPhoneアプリ開発。objective-CでHelloWorldを出力というタイトルどおり、HelloWorldを表示させてみます。
InterfaceBuilder(xib)を使って作ってもなんにもおもしろくないのでコードで実装します。
参考にさせてもらった本はオライリーの
iPhone SDK アプリケーション開発ガイド
です。
さすがオライリー本。
プログラムの処理順やそれぞれの処理の説明も詳しく書かれててこの本かなりいいです。
Xcode起動して
新規プロジェクト作成で、
iOS > Application > Window-based-Application
を選択します。プロジェクト名はHelloViewにします。
classes/HelloViewAppDelegate.h
#import <UIKit/UIKit.hW> #import <UIKit/UITextView.h> @interface MainView : UIView { UITextView *textView; } -(id)initWithFrame:(CGRect)rect; -(void)dealloc; @end @interface HelloViewAppDelegate : NSObject &lt;UIApplicationDelegate&gt; { UIWindow *window; MainView *myMainView; } @property (nonatomic, retain) IBOutlet UIWindow *window; @end
classes/HelloViewAppDelegate.m
#import "HelloViewAppDelegate.h" @implementation MainView -(id)initWithFrame:(CGRect)rect { //at first, initWithFrame of super class is called. then UIView object is reset. self = [super initWithFrame:rect]; //if object is reset, self is nil if (self != nil){ //myclass vars is reset. textView = [[UITextView alloc] initWithFrame:rect]; textView.text = @"Hello World?"; textView.font = [UIFont fontWithName:@"Arial" size:32.0f]; //myclass first resource is assets. [self addSubview:textView]; } return self; } -(void) dealloc { //myclass resouce is released. //super class method is called.then UIView resource is released. [super dealloc]; } @end @implementation HelloViewAppDelegate @synthesize window; - (void) applicationDidFinishLaunching:(UIApplication *)application{ //CGRect CGRect screenBounds = [[UIScreen mainScreen] applicationFrame]; CGRect viewRect = screenBounds; viewRect.origin.x = viewRect.origin.y = 0; // window reset window = [[UIWindow alloc] initWithFrame:screenBounds]; // view reset myMainView = [[MainView alloc] initWithFrame:viewRect]; // view anchor to window [window addSubview:myMainView]; // point window as mainwindow, then output it. [window makeKeyAndVisible]; } - (void)dealloc { [myMainView release]; [window release]; [super dealloc]; } @end
main.m
#import <UIKit/UIKit.h> int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, @"HelloViewAppDelegate"); [pool release]; return retVal; }
で、ビルド/実行するとHello Worldが出力されます。
各コードの意味や目的や説明などは、iPhone SDK アプリケーション開発ガイド
にめちゃ詳しく書かれてるので読んでみると理解しやすいと思います。
「iPhoneアプリ開発はじめたい、けどこれは読んでない」って方にオススメしたいです。
[tmkm-amazon]4873114179[/tmkm-amazon]