SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

DeveloperZine(デベロッパージン)- エンジニアの意思決定を支える技術情報メディア ProductZine

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

スマホアプリ開発の最新トレンド! ぜったい外せない位置情報活用術

YOLPで挑戦~目的地を地図で表示するiOSアプリを、 地図ライブラリを使わずにお手軽に作る!

スマホアプリ開発の最新トレンド! ぜったい外せない位置情報活用術(5)

開発手順

新規プロジェクトの作成

 まずは今回のアプリケーションを作成するためのプロジェクトを作りましょう。

 Xcodeを起動し、メニューバーから[File]-[New]-[New Project](Xcode4.3では[Project])を選ぶと、新しいプロジェクトのテンプレート選択画面が表示されます(「Welcome to Xcode」が表示された場合は、その中の「Create a new Xcode project」でも可)。

 テンプレートは「Empty Application」を選択してください。

 今回のアプリケーションでは、Product Nameは「GeoCoding」とします。

 またCore DataやAutomatic Reference Countingなどを使わないプログラムのコードを書いていくので、これらのチェックは外してください。

 プロジェクトに対しては以上です。特に外部ライブラリやフレームワークを追加する必要はありません。

キーワード入力画面の作成

 [File]-[New]-[New File](Xcode4.3では[File])を選んで、新しいファイルのテンプレート選択画面を表示します。

 テンプレートは「UIViewController subclass」(Xcode4.3では「Objective-C class」)を選択し、クラス名はRootViewController、サブクラスはUIViewControllerでファイルを作ります。

 ファイルが準備できたら、以下のようにソースコードを書き換えます。

RootViewController.h
#import <UIKit/UIKit.h>
 
@interface RootViewController : UIViewController <UITextFieldDelegate> {
  UITextField *_textField;
}
 
@end
RootViewController.m
#import "RootViewController.h"
 
@implementation RootViewController
 
- (id)init {
  self = [super init];
  if (self) {
    _textField = nil;
  }
  return self;
}
 
- (void)dealloc {
  [_textField release];
  [super dealloc];
}
 
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
}
 
- (void)viewDidLoad {
  [super viewDidLoad];
  self.title = @"キーワード入力";
 
  //テキストフィールド
  if(_textField == nil){
    //サイズや位置は適当に
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 50.0f, 300.0f, 44.0f)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.returnKeyType = UIReturnKeySearch;
    _textField.delegate = self;
  }
  [self.view addSubview:_textField];
   
  //決定ボタン
  UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [submitButton setTitle:@"submit" forState:UIControlStateNormal];
  //サイズや位置は適当に
  submitButton.frame = CGRectMake(10.0f, 94.0f, 300.0f, 44.0f);
  [submitButton addTarget:self action:@selector(tapSubmit:) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:submitButton];
}
 
- (void)viewDidUnload {
  [super viewDidUnload];
}
 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 
- (BOOL)submit{
  [_textField resignFirstResponder];
 
  return YES;
}
 
//決定ボタンの挙動を定義
- (void)tapSubmit:(id)sender {
  [self submit];
}
 
//キーボードの「検索」でも実行できるようにする
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  return [self submit];
}
 
@end

 これでキーワード入力画面ができます。

 後はこれを読み込むためにAppDelegate.mを一部書き換えます。

AppDelegate.m
#import "AppDelegate.h"
//作成した画面を使えるようにimport文を追加
#import "RootViewController.h"
 
----- 中略 -----
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  RootViewController *rootViewController = [[[RootViewController alloc] init] autorelease];
  UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
  [self.window setRootViewController:navigationController];
  [self.window makeKeyAndVisible];
  return YES;
}
 
----- 以下略 -----

 ここまでで、まずは入力画面が出来上がります。

次のページ
Yahoo!ジオコーダAPIについて

この記事は参考になりましたか?

スマホアプリ開発の最新トレンド! ぜったい外せない位置情報活用術連載記事一覧

もっと読む

この記事の著者

宮川 博幸(ヤフー株式会社)(ミヤカワ ヒロユキ(ヤフーカブシキガイシャ))

メディアサービスカンパニー 開発本部所属。2005年度のACM主催のプログラミングコンテストでヤフーのエンジニアと話したことがきっかけとなり、2007年にヤフーに入社。道路交通情報、地図、路線にかかわり、現在はiOS版「地図 Yahoo!ロコ」アプリの開発を主とする。好きな言葉は「全ての道はローマに...

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

CodeZine(コードジン)
https://codezine.jp/article/detail/6711 2012/08/23 14:00

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー