@neiraza

2匹の怪獣が寝た後にプログラマーしてる、最近はサイバーエージェントでゼミ長もしてる僕のネタ帳

iPhoneアプリ開発講座を受講する その1

[iPhone][Objective-C]

2012年はiPhoneアプリ開発を中心にやっいこうかなと。

理由としては、

  • Androidアプリ開発との違いを実感したくなってきた
  • Mac含め、Apple製品が欲しくなってきたから、その動機付けとして
  • 今年の初めにJavaFX1.3でメディアプレイヤーを実装してから趣向が変わった

色々あるけど、2012年からはモバイルアプリ開発を中心に、
そっち側の視点から必要に応じて、サーバーサイドの開発もと思ってる。
ま、あんまりこの区分け自体に意味はないんだけど。

というわけで、現在はiPhoneアプリ開発講座に参加してる。
サイバーエージェントさんが開催している、cocos2d講座だ。
細かい内容は外で話さないという誓約をしているので、
自分が出来るようになった事を中心に、振り返りメモ代わりに書いていこうと思う。

その講座を受けるには「筆記試験」と「面接」があった代わりに、受講料は無料。
全10回で、最初の2回が前の土日にあって、Objective-C基礎をやってきた。

今日はその振り返りをしようと思う。


そもそも、Macを初めて触ってから1週間だったので、慣れないこともちらほら。
言語も開発環境も知らない事がどっさり。

プロジェクトの作成

template選択のところで、iOS=>Application=>Empty Application
最初は必要ないとのことで、[Core Data]、[ARC]、[UT]はチェックボックスを空にしとく。
ARC(Automatic Reference Counting):なんでも自分で管理しないでもreleaseしてくれるとか?

最初はUIViewControllerを作成して、そいつにボタンとラベルを置いてみよう。

UIViewControllerの生成

File=>New=>New File=>UIViewController subclassでポンとできる。

あっちゃこっちゃで使うからと、AppDelegate.hをこんな感じで書いてみた。

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    RootViewController * rootViewController_;
}
@property (strong, nonatomic) UIWindow *window;

@end


続いて、AppDelegate.mはこんな感じ。
RootViewControllerってのを自分で生成してみた。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
<span style="color: #0000cc">    rootViewController_ = [[RootViewController alloc] init];
    [rootViewController_.view setFrame:CGRectMake(0, 20, 320, 460)];
    [self.window addSubview:rootViewController_.view];</span>
    
    return YES;
}

このrootViewContoller_のメモリ解放。

- (void)dealloc
{
    [_window release];
    [rootViewController_ release];
    [super dealloc];
}

ラベルとボタン

RootViewController.h

#import <UIKit/UIKit.h>
#import "ScoreManager.h"
@interface RootViewController : UIViewController<ScoreManagerDeleagate>
{
    UILabel * scoreLabel_;
    ScoreManager * scoreManager_;
}

@end

RootViewController.m

    scoreLabel_ = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    [scoreLabel_ setText:@"SCORE"];
    [self.view addSubview:scoreLabel_];
    
    UIButton * scoreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [scoreButton setFrame:CGRectMake(0, 50, 100, 50)];
    [scoreButton setTitle:@"Add" forState:UIControlStateNormal];
    [scoreButton addTarget:self action:@selector(didTap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:scoreButton];

f:id:redogu:20111220220626p:plain

こんな感じ!