やることは、View-based Application のテンプレートでプロジェクトを作成して、ナビゲーション機能を実装するところまで。
1.「View-based Application」でテンプレートを作成。
2.プロジェクトが作成される。
3.次に「New File」で新しいビューを作成する。これがないと始まらない。
4.今回は UIViewController で作成する。
5.適当に名前をつけて作成する。(今回は NewViewController とした)
6.プロジェクトにファイルが作成される。
ひとまずここで一区切り。
次に少々コードを書いて下準備をする。
主要部分だけを説明していく。赤字の部分がポイント箇所。
◎WindowBasedAppDelegate.h
#import <UIKit/UIKit.h>
@interface WindowBasedAppDelegate : NSObject{
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
◎WindowBasedAppDelegate.m
#import "WindowBasedAppDelegate.h"
@implementation WindowBasedAppDelegate
@synthesize window=_window;
@synthesize navigationController=_navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[_navigationController release];
[_window release];
[super dealloc];
}
@end
コードの記述終了!短かった。。。
また次からマウスを使った作業に入る。
7.「MainWindow.xib」に移動して、「Navigation Controller」を追加する。
8.Outlets の「navigationController」を画像のように紐付ける。
9.「View Controller - Root View Controlelr」の NIB Name を「NewViewController」に設定する。
10.「View Controller - Root View Controlelr」の Class を「UIViewController」から「NewViewController」に変更する。
以上。
あとは新規ビューを追加してpush処理をしてあげると、ナビゲーションしてくれるようになる。
うーん・・・せっかくなのでそこまでやってしまおうかな。。。
1.「New File」で新しいビュー(SecondViewController)を作成する。
2.NewViewController.h、NewViewController.m に手を入れていく。
主要部分だけを説明していく。赤字の部分がポイント箇所。
◎NewViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface NewViewController : UIViewController {
}
@end
◎NewViewController.m
#import "NewViewController.h"
@implementation NewViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// ボタンを追加
UIButton *bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
bt.frame = CGRectMake(110, 150, 100, 30);
[bt setTitle:@"遷移する" forState:UIControlStateNormal];
[bt addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchDown];
[self.view addSubview:bt];
}
// ボタンアクション
-(void)buttonAction:(UIButton*)button
{
SecondViewController *secondViewController;
secondViewController = [[SecondViewController alloc]
initWithNibName:@"SecondViewController"
bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
}
@end
以上。
SecondViewControllerには手は入れていない。
■完成画像
↓ 「遷移する」ボタンを押下
----------
サンプルソース:https://github.com/tetsuco/WindowBased