「なぜこの代替が必要なのですか?」 これは、SplitViewの動作に関するものです。SDKからの抜粋です。「両方のView Controllerは横向きで表示されますが、Detail View Controllerのみが縦向きで表示されます」。 つまり 縦向きでは、右側(MasterView)は単純に非表示になります(図1を参照)。


図1-横(a)および縦(b)方向のSplitView
iPadのメイン設定を見ると、まったく異なる画像が表示されます。縦向きと横向きでは、左側の部分(MasterView)が同じように表示されます(非表示になりません)

図2-基本的なiPad設定
さて、
Xcode(ビューベースのアプリケーション)で新しいプロジェクトを作成することから始めましょう。 AlternativeSplitという名前を付けました(図3を参照)。

図3-新しいプロジェクトの作成
次に、メニューからFile-> New file ...-> Cocoa Touch Class-> UIViewControllerサブクラスを選択して、MasterViewController(UITableViewControllerのサブクラス)とDetailViewController(UIViewControllerのサブクラス)の2つの新しいクラスをプロジェクトに追加します。
InterfaceBuilderでAlternativeSplitViewController.xibを開き、「このような絵を描く」:

図4-AlternativeSplitViewController.xibの編集
ある方向から別の方向に切り替えるときにビューが「フロート」しないように、自動サイズ設定を構成します。 マスターの左側(NavigationBarとTableView)には、図5.aと同じパラメーターを指定し、右側の詳細(NavigationBarとView)には、図5 5.b


図5-自動サイズ設定オプション
ファイルを編集して、AlternativeSplitViewControlle.hクラスに新しいIBOutlet変数を追加します
次のAlternativeSplitViewController.h
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
// // AlternativeSplitViewController.h // AlternativeSplit // #import <UIKit/UIKit.h> @ class MasterViewController; @ interface AlternativeSplitViewController : UIViewController { IBOutlet MasterViewController* masterViewController; } @end * This source code was highlighted with Source Code Highlighter .
MasterViewControllerクラスは、メニュー項目を含むテーブルを担当します。 次のように書き直します(要するに、以下のコードではテーブルにデータを入力し、ユーザーがテーブルセル(didSelectRowAtIndexPath)を選択するとDetailViewメッセージを送信します):
*このソースコードは、 ソースコードハイライターで強調表示されました。
- //
- // MasterViewController.h
- // AlternativeSplit
- //
- #import <UIKit / UIKit.h>
- @ class DetailViewController;
- @ インターフェイス MasterViewController:UITableViewController {
- IBOutlet DetailViewController * detailViewController;
- }
- @end
*このソースコードは、 ソースコードハイライターで強調表示されました。
- //
- // MasterViewController.m
- // AlternativeSplit
- //
- #import "MasterViewController.h"
- #import "DetailViewController.h"
- @implementation MasterViewController
- #プラグママーク-
- #pragma markライフサイクルを表示
- -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- //オーバーライドして、デフォルトの縦向き以外の向きを許可します。
- YESを返します。
- }
- #プラグママーク-
- #pragma markテーブルビューデータソース
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- //セクションの数を返します。
- 1を返します。
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)セクション{
- //セクション内の行数を返します。
- return 5;
- }
- //テーブルビューセルの外観をカスタマイズします。
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString * CellIdentifier = @ "Cell" ;
- UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil){
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reloadIdentifier:CellIdentifier] autorelease];
- }
- //セルを構成します...
- cell.textLabel.text = [NSString stringWithFormat: @ "Row%i" 、indexPath.row];
- 戻りセル。
- }
- #プラグママーク-
- #pragma markテーブルビューデリゲート
- -( void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [detailViewController setNewText:[NSString stringWithFormat: @ "Row%i" 、indexPath.row]];
- }
- #プラグママーク-
- #pragma markメモリ管理
- -( void )didReceiveMemoryWarning {
- //ビューにスーパービューがない場合、ビューを解放します。
- [super didReceiveMemoryWarning];
- //使用されていないキャッシュデータ、画像などの所有権を放棄します。
- }
- -( void )dealloc {
- [super dealloc];
- }
- @end
DetailViewControllerが適切な部分を担当します。 DetailViewControllerクラスを編集しましょう(新しいIBOutlet変数UILabel * lblと、lblのテキストを変更する新しいsetNewTextメソッドを追加します)
*このソースコードは、 ソースコードハイライターで強調表示されました。
- //
- // DetailViewController.h
- // AlternativeSplit
- //
- #import <UIKit / UIKit.h>
- @ インターフェース DetailViewController:UIViewController {
- IBOutlet UILabel * lbl;
- }
- -( void )setNewText:(NSString *)テキスト;
- @end
*このソースコードは、<a href="http://virtser.net/blog/post/source-code-highlighter.aspx"> ソースコードハイライターで強調表示されました 。
- //
- // DetailViewController.m
- // AlternativeSplit
- //
- #import "DetailViewController.h"
- @implementation DetailViewController
- -( void )setNewText:(NSString *)テキスト{
- lbl.text = text;
- }
- -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- //任意の方向を許可するためにオーバーライドされます。
- YESを返します。
- }
- -( void )didReceiveMemoryWarning {
- //ビューにスーパービューがない場合、ビューを解放します。
- [super didReceiveMemoryWarning];
- //使用されていないキャッシュデータ、画像などを解放します。
- }
- -( void )viewDidUnload {
- [super viewDidUnload];
- //メインビューの保持されているサブビューを解放します。
- //例:self.myOutlet = nil;
- }
- -( void )dealloc {
- [super dealloc];
- }
- @end
あとは、interfaceBuildereでIBOutlet変数をバインドするだけです(図6を参照)。 そして、問題は帽子です。

図6-IBOutlet変数のバインド
結果:


図7-別のSplitView
ソースコードはこちらまたはこちらからダウンロードできます 。
ご清聴ありがとうございました。
PSこの記事には、「手を少し動かすと、UITabBar