開発手順
新規プロジェクトの作成
まずは今回のアプリケーションを作成するためのプロジェクトを作りましょう。
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でファイルを作ります。
ファイルが準備できたら、以下のようにソースコードを書き換えます。
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController <UITextFieldDelegate> {
UITextField *_textField;
}
@end
#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を一部書き換えます。
#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;
}
----- 以下略 -----
ここまでで、まずは入力画面が出来上がります。




