ちょっとイマジネーションが湧いてきたんで、UISearchbarを使いたい

iPhoneのアプリ作りたいなぁと思いつつ、何も作りたいものが湧いてきて来なかったんですが
少し、アイディアが出てきました!

なんで、それに使いたいなぁと思う検索バー(UISerachBar)を出してみたいと思います。

構成としてはプロジェクトは「Windows-Based Application」で「HogeHoge」と作成し、
サブクラスとして「UIviewController subClass」でViewControllerを作成したものとしてください。

できたファイル構成はこれです。今回もInterface Builderは使いません。

  • HogeHogeAppDelegate.h
  • HogeHogeAppDelegate.m
  • TopViewController.h
  • TopViewController.m

もう、後は簡単にソースを。

TopViewController.h

#import <UIKit/UIKit.h>


@interface TopViewController : UIViewController <UISearchBarDelegate> {
	UISearchBar		*searchBar;
}

TopViewController.m

#import "TopViewController.h"

@implementation TopViewController

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	CGRect bounds = [[UIScreen mainScreen] applicationFrame];
	
	searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, bounds.size.width, 48.0)];
	searchBar.delegate = self;
	searchBar.placeholder = @"キーワードを入れてください";
	[self.view addSubview:searchBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

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

@end

HatenaKeywordAppDelegate.h

#import <UIKit/UIKit.h>
#import "TopViewController.h"

@interface HatenaKeywordAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    TopViewController *topViewController;
}

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

@end

HatenaKeywordAppDelegate.m

#import "HatenaKeywordAppDelegate.h"
#import "TopViewController.h"

@implementation HatenaKeywordAppDelegate

@synthesize window;
@synthesize topViewController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
    topViewController = [[TopViewController alloc] init];
	
    // Override point for customization after application launch
    [window addSubview:topViewController.view];
    [window makeKeyAndVisible];
}


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


@end

これでコンパイルするとこんな感じになりました。
まだ、検索バーがでてるだけですが、ここから少し足していきたいなぁと思います。