2012年07月07日

Objective-C 入門 [Hello worldプログラム]

言語:Objective-C
OS:Linux (Fedora 15)

世間からは完全に周回遅れになってしまいましたが、最近iPadを触る機会があり、とても良いものだと思ったので、がんばってiPad(iPhone)アプリを作れるようになろうと考えたりしています。

そこで、開発言語であるObjective-Cからやってみることにしました。
(※実は2~3年前にiPhoneアプリ開発の勉強をしたことがあるのですが、SDKについていけず挫折しました。)

今回は言語から少しづつ攻めていこうという作戦です。

Objective-Cは言語自体に癖があるので、普段JavaとかPHPとかC/C++とかやっている人は戸惑うと思います。
例として、C/C++との違いをまとめてみます。
違い1:#include <hoge.h>じゃなくて#import <hoge.h>
違い2:classじゃなくて@interface ~ @end

特に、違い2は独特です。

最初は慣れることが肝心だという考えのもとに、おなじみのHello worldプログラムを実装してみました。
#import <stdio.h>
#import <objc/Object.h>

@interface Hello : Object
  - (void)hello;
@end

@implementation Hello
  - (void)hello
  {
    printf("Hello world\n");
  }
@end

int main()
{
  id obj = [Hello alloc];
  [obj hello];

  return 0;
}

クラスの定義は@interface ~ @endでやって、その中身を@implementation ~ @endに書きます。普通はヘッダー(***.h)と実装(***.m)に分けて書きますが、面倒なので、今回は1つのファイルに書いています。

さて、コンパイルですが、Objective-Cはgccでコンパイルできます。

私の環境ではgccは入っていたのですが、Objective-Cをコンパイルできるヤツではなかったみたいなので、yumでいれました。以下はそのコマンドです。
> yum install gcc-objc

Linuxならこれで入ると思います。

次にコンパイル(とリンク)をします。
コンパイルは"gcc -c ファイル名"です。
> gcc -c hello.m
これで、オブジェクトファイル(hello.o)が生成されます。

最後に、リンクをして実行ファイル(hello)を生成します。
> gcc -l objc -o hello hello.o
objcをリンクしないとエラーになります。

せっかく実行ファイルができたので、実行してみます。
> ./hello
Hello world


    • 0 Comment |
    • 0 Trackback |
    • このエントリーをはてなブックマークに追加
2010年01月25日

UITableViewに必要なUITableViewDataSourceプロトコルのメソッド-(Mac OS X, iPhone, Objective-C)

CA3A0063

iPhone SDK プログラミング [UITableViewの利用] - (iPhone[Mac OS X], Objective-C)」の続き。

今回は、UITableViewDataSourceプロトコルの必須メソッドについて。
テーブルを使用するにあたって、必ず実装しなければならないものは、以下の2つのメソッドです。
(1) -(NSInteger)tableView:(UITableView*)tableView
         numberOfRowsInSection:(NSInteger)section

(2) -(UITableViewCell*)tableView:(UITableView*)tableView
         cellForRowAtIndexPath:(NSIndexPath*)indexPath

です。
(1)はテーブルに表示する行数を取得するためのメソッドで、(2)は表示するセルを取得するメソッドです。

さらに、セクションを使う場合は、以下の2つのメソッドを実装します。
(3) -(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
(4) -(NSString*)tableView:(UITableView*)tableView
         titleForHeaderInSection:(NSInteger)section

です。
(3)はセクションの数を取得します。デフォルトでは1を返します。(4)はセクションのタイトルを取得します。実装しない場合、タイトルは表示されません。

    • 0 Comment |
    • 0 Trackback |
    • このエントリーをはてなブックマークに追加
2010年01月24日

iPhone SDK プログラミング [UITableViewの利用] - (iPhone[Mac OS X], Objective-C)

CA3A0024

OS:Max OS X(iPhone)
言語:Objective-C

せっかくMacBookに移行したので、iPhoneアプリのプログラミングを勉強することにした。

iphone_table左図のようなユーザインタフェースをテーブルをいう。今回は、左図と同じテーブルを実装してみる。以下の手順で行う。


1.X-codeを起動して、「Window-Based Application」の新規プロジェクトを作成する。プロジェクト名は「table」とした。


2.tableAppDelegate.hにUITableViewDataSourceというプロトコルを追加する。このプロトコルはテーブルに表示するデータを扱うメソッドを定義しているらしい。以下はコードです(赤字の部分が追加したコード)。


@interface tableAppDelegate : NSObject <UIApplicationDelegate, UITableViewDataSource>
{
    NSArray* names1;
    NSArray* names2;
    UIWindow* window;
}

@property (nonatomic, retain) IBOutlet UIWindow* window;

@end


3.InterfaceBuilderでユーザインタフェースを作成する。InterfaceBuilderのLibraryウィンドウからTableViewをドラッグ&ドロップで貼付ける。

4.taleAppDelegate.mファイルを編集する。テーブルを使うろきの必須メソッドとセクションに関連するメソッドを実装する。以下はコードです(赤字は追加したコード)。

#import "tableAppDelegate.h"

@implementation tableAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    names1 = [[NSArray arrayWithObjects:@"Cell1", @"Cell2", @"Cell3", @"Cell4", nil] retain];
    names2 = [[NSArray arrayWithObjects:@"Cell1", @"Cell2", @"Cell3", nil] retain];

   
    // Override point for customization after application launch
    [window makeKeyAndVisible];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return 2;
}


- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
    switch ( section ) {
        case 0:
            return @"Section1";
            break;
           
        case 1:
            return @"Section2";
            break;
    }
   
    return nil;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    switch ( section ) {
        case 0:
            return [names1 count];
            break;
           
        case 1:
            return [names2 count];
            break;
    }
   
    return 0;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
   
    if ( !cell ) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"UITableViewCell"];
        [cell autorelease];
    }
   
    switch ( indexPath.section ) {
        case 0:
            cell.text = [names1 objectAtIndex:indexPath.row];
            break;
           
        case 1:
            cell.text = [names2 objectAtIndex:indexPath.row];
            break;
    }
   
    return cell;
}


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

@end



5.コンパイルして、結果を確認する(左上のように表示されるはず)。

    • 0 Comment |
    • 0 Trackback |
    • このエントリーをはてなブックマークに追加