View-based Application から Navigation を作る | 成長の果実

成長の果実

不完全でも良いから前に進む。

いつも View-based Application から作成するけど、たまにやり方忘れるのでメモ。

やることは、View-based Application のテンプレートでプロジェクトを作成して、ナビゲーション機能を実装するところまで。


1.「View-based Application」でテンプレートを作成。
$成長の果実-WindowBased01

2.プロジェクトが作成される。
成長の果実-WindowBased02

3.次に「New File」で新しいビューを作成する。これがないと始まらない。
成長の果実-WindowBased03

4.今回は UIViewController で作成する。
成長の果実-WindowBased04

5.適当に名前をつけて作成する。(今回は NewViewController とした)
成長の果実-WindowBased05

6.プロジェクトにファイルが作成される。
成長の果実-WindowBased06



ひとまずここで一区切り。


次に少々コードを書いて下準備をする。

主要部分だけを説明していく。赤字の部分がポイント箇所。



◎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」を追加する。
成長の果実-WindowBased07

8.Outlets の「navigationController」を画像のように紐付ける。
成長の果実-WindowBased08

9.「View Controller - Root View Controlelr」の NIB Name を「NewViewController」に設定する。
成長の果実-WindowBased10

10.「View Controller - Root View Controlelr」の Class を「UIViewController」から「NewViewController」に変更する。
成長の果実-WindowBased09


以上。


あとは新規ビューを追加して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には手は入れていない。


■完成画像
成長の果実-WindowBased_view01

        「遷移する」ボタンを押下

成長の果実-WindowBased_view02


----------
サンプルソース:https://github.com/tetsuco/WindowBased