<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iphoneアプリで稼げるのか &#187; Interface Builder</title>
	<atom:link href="http://iphone.longearth.net/tag/interface-builder/feed/" rel="self" type="application/rss+xml" />
	<link>http://iphone.longearth.net</link>
	<description></description>
	<lastBuildDate>Sun, 08 Aug 2010 21:05:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>【Xcode】設定しておくと便利なカスタマイズいろいろ</title>
		<link>http://iphone.longearth.net/2009/08/09/%e3%80%90xcode%e3%80%91%e8%a8%ad%e5%ae%9a%e3%81%97%e3%81%a6%e3%81%8a%e3%81%8f%e3%81%a8%e4%be%bf%e5%88%a9%e3%81%aa%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%9e%e3%82%a4%e3%82%ba%e3%81%84%e3%82%8d%e3%81%84/</link>
		<comments>http://iphone.longearth.net/2009/08/09/%e3%80%90xcode%e3%80%91%e8%a8%ad%e5%ae%9a%e3%81%97%e3%81%a6%e3%81%8a%e3%81%8f%e3%81%a8%e4%be%bf%e5%88%a9%e3%81%aa%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%9e%e3%82%a4%e3%82%ba%e3%81%84%e3%82%8d%e3%81%84/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 02:21:13 +0000</pubDate>
		<dc:creator>daichi</dc:creator>
				<category><![CDATA[開発補助]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[objecti]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[Three20]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[Xcode]]></category>
		<category><![CDATA[アプリ]]></category>
		<category><![CDATA[ダウンロード]]></category>
		<category><![CDATA[デバッグ]]></category>
		<category><![CDATA[リリース]]></category>

		<guid isPermaLink="false">http://iphone.longearth.net/?p=1306</guid>
		<description><![CDATA[
いろいろカスタマイズして便利にします。
外観
デバッグコンソールなどを１つのウィンドウに統合する
シミュレータでアプリを起動すると、デバッガコンソールがXcodeの後ろに隠れてしまい、いちいちフォーカスを切り替えるのが [...]]]></description>
			<content:encoded><![CDATA[<p>
いろいろカスタマイズして便利にします。<br />
<h3>外観</h3>
<h4>デバッグコンソールなどを１つのウィンドウに統合する</h4>
シミュレータでアプリを起動すると、デバッガコンソールがXcodeの後ろに隠れてしまい、いちいちフォーカスを切り替えるのが面倒。そんなときはこの設定を。<br />
Xcode→環境設定→全般→レイアウト→オールインワン<br />
<h4>アプリ実行時にデバッガを自動的に表示する</h4>
Xcode→環境設定→デバッグ→開始時→コンソールとデバッガを表示<br />
<h4>__MY_COMPANYNAME__を変更する</h4>
ファイルを新規に作成する時にファイル作成者の情報等が自動生成されるが__MY_COMPANYNAME__が気持ち悪い。そんな時は、コンソールから以下のコマンドを叩く。YourNameHereを表示したい名前に置き換えて。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1306code6'); return false;">View Code</a> SHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p13066"><td class="code" id="p1306code6"><pre class="shell" style="font-family:monospace;">defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions
   '{ORGANIZATIONNAME=&quot;YourNameHere&quot;;}'</pre></td></tr></table></div>

<br />
<br />
<br />
<h3>プリプロセッサ</h3>
<h4>デバッグ用マクロ</h4>
デバッグ時はNSLogメソッドをよく使うが、リリースビルドにはNSLogを使いたくない。そんな時はプリプロセッサでDebugモードの時だけ、NSLogを吐くマクロを定義すればいい。<br />
Global.h<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1306code7'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p13067"><td class="code" id="p1306code7"><pre class="objective-c" style="font-family:monospace;">#ifdef DEBUG 
#define LOG(...) NSLog(__VA_ARGS__)
#else
#define LOG(...) 
#endif</pre></td></tr></table></div>

<br />
<br />
<br />
info.plistでDebug構成時のみ、GCC_PREPROCESSOR_DEFINITIONS項目にDEBUG文字列を設定する。<br />
<br />
すると、LOG(@&#8221;hogehoge&#8221;)でDebug構成時のみ、NSLogを吐けるようになる。<br />
<br />
<h3>テンプレート</h3>
<h4>IBを使わないテンプレートを作成する</h4>
Interface Builderは最近は使わなくなったので、新しいプロジェクトを作る場合に、いちいち削除したりするのが面倒。そんな時はプロジェクトテンプレートをカスタマイズする。<br />
<br />
Xcodeのプロジェクトテンプレートは、以下の場所においてある。<br />
/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application<br />
<br />
ユーザ定義のテンプレートは以下の場所に置くとXcodeに認識される。<br />
~/Library/Application Support/Developer/Shared/Xcode/Project Templates/Application/<br />
<br />
Windowベースのアプリケーションをベースにカスタマイズする。オリジナルのWindow-based Applicationをユーザ定義の方にコピーしてWindow-based-non-IB Applicationにリネーム。<br />
<br />
ディレクトリ内の___PROJECTNAME___はすべてプロジェクト名に置き換えられる。<br />
<br />
変更するのは以下。<br />
<ul>
	<li>info.plistのMain nib file base name→空に</li>
	<li>main.m内のUIApplicationMainメソッドの第４匹数に@&#8221;___PROJECTNAME___AppDelegateを&#8221;</li>
	<li>MainWindow.xib→削除</li>
	<li>___PROJECTNAME___.xcodeproj/project.pbxproj→MainWindow.xibに関する部分をすべて削除</li>
</ul>
これでxibファイル抜きのテンプレートができあがり。<br />
<br />
個人的に___PROJECTNAME___AppDelegateという名前が長ったらしくてあまり好きではないので、これをAppDelegateにした。<br />
この時の変更箇所は以下。<br />
<ul>
	<li>main.m→@&#8221;___PROJECT_NAME___AppDelegate&#8221;を@&#8221;AppDelegate&#8221;へ</li>
	<li>___PROJECT_NAME___AppDelegate.h→ファイル名をAppDelegate.hへ。中身も該当箇所を修正</li>
	<li>___PROJECT_NAME___AppDelegate.m→ファイル名をAppDelegate.mへ。中身も該当箇所を修正</li>
	<li>___PROJECT_NAME___.xcodeproj/project.pbxproj→___PROJECT_NAME___AppDelegate部分をすべてAppDelegateへ修正</li>
</ul>
<h4>.gitignore、.gitattributeをテンプレートに含める</h4>
gitを使いだすとgitの設定ファイルである.gitignoreでXcode用ファイル達を除外したくなるが、これを毎回プロジェクトを作る度にコピーしてくるのは面倒なので、テンプレートに含めてしまう。やり方は上のテンプレートディレクトリに.gitignore、.gitattributeファイルを入れるだけ。<br />
.gitignoreは<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1306code8'); return false;">View Code</a> SHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p13068"><td class="code" id="p1306code8"><pre class="shell" style="font-family:monospace;"># xcode noise
build/*
*.pbxuser
*.mode1v3
&nbsp;
# old skool
.svn
&nbsp;
# osx noise
.DS_Store
profile</pre></td></tr></table></div>

<br />
<br />
.gitattributesは<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1306code9'); return false;">View Code</a> SHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p13069"><td class="code" id="p1306code9"><pre class="shell" style="font-family:monospace;">*.pbxproj -crlf -diff -merge</pre></td></tr></table></div>

<br />
<br />
ここまでのテンプレート設定ディレクトリをいちおのせておく。これをダウンロードして、ローカルの<br />
~/Library/Application Support/Developer/Shared/Xcode/Project Templates/Application/<br />
に置けば、使えると思う。<br />
<a href='http://iphone.longearth.net/wp-content/uploads/2009/08/Window-based-non-IB-Application2.zip'>Window-based-non-IB Application</a><br />
これには次のログマクロテンプレートも含まれている。<br />
<h4>ログマクロをテンプレートに含める</h4>
さきほど作ったログマクロをテンプレートに含めるのは、テンプレートディレクトリ内にログマクロを記述したファイルを追加すればOKだが、ファイルを開発時にファイルを新規作成するたびに、<br />
#import &#8220;Global.h&#8221;<br />
とするのは面倒だ。<br />
<br />
なので、ファイル作成時に既に#import &#8220;Global.h&#8221;を追記されているファイルテンプレートを作成する。<br />
<br />
オリジナルのファイルテンプレートは以下に置いてある。<br />
/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/File Templates/Cocoa Touch <br />
<br />
このCocoa Touchディレクトリをユーザ定義用ファイルテンプレートディレクトリである<br />
~/Library/Application Support/Developer/Shared/Xcode/File Templates/<br />
へコピー。<br />
<br />
あとはファイルを好きなように変更すればそれがテンプレートになる。<br />
<br />
Cocoa Touchのすべてのファイル作成時にGlobal.hを含むように設定したファイルテンプレートはこれ。<br />
<a href='http://iphone.longearth.net/wp-content/uploads/2009/08/Cocoa-Touch-Class.zip'>Cocoa Touch Class</a><br />
これを<br />
~/Library/Application Support/Developer/Shared/Xcode/File Templates/<br />
へ入れれば、ユーザ定義ファイルとして使える。<br />
<br />
<br />
<h4>よく使うフレームワークをテンプレートに含める</h4>
例えば、Three20だったり、JSONフレームワークだったり、GTMだったり、こういうよく使うフレームワークはあらかじめ使える準備の整ったプロジェクトテンプレートが欲しくなる。これも上のやり方同様、コピーしたローカルのテンプレート周りをいじくり倒して設定できる。<br />
<h3>マクロ</h3>
<h4>よく使うメソッドをマクロとして登録する</h4>
Xcodeでは、Ctrl+.でマクロを呼び出せる。<br />
例えばinitと打って、Ctrl+.を押すと、NSObjectのinitメソッドが挿入される。他にもlog、deallocなどが予めマクロとして登録されている。<br />
が、ViewController周りのメソッド達は登録されていないので、いちいちviewWillAppear〜などとよく使うメソッドを入力しなければならない。これは面倒だ。ということでマクロを登録する。<br />
<br />
オリジナルのマクロ定義ファイルは以下にある。<br />
/Developer/Applications/Xcode.app/Contents/PlugIns/TextMacros.xctxtmacro/Contents/Resources/ObjectiveC.xctxtmacro<br />
<br />
これをユーザ定義のマクロファイル置き場である以下にコピーする。<br />
~/Library/Application Support/Developer/Shared/Xcode/Specifications<br />
<br />
マクロとして<br />
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath<br />
を追加する場合は、ファイル内に以下のような記述を追加する。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1306code10'); return false;">View Code</a> SHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p130610"><td class="code" id="p1306code10"><pre class="shell" style="font-family:monospace;">        {
            Identifier = objc.didselect;
            BasedOn = objc;
            IsMenuItem = NO;
            Name = &quot;didSelectRowAtIndexPath&quot;;
            TextString = &quot;- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {¥n¥t&amp;lt;#statements#&amp;gt;¥n}&quot;;
            CompletionPrefix = didselect;
        },</pre></td></tr></table></div>

<br />
<br />
内容の説明<br />
<ul>
	<li>Identifier→ユニークな識別ID。</li>
	<li>Name→Xcodeで編集→テキストマクロを挿入→Objective Cと行った時に表示される名称</li>
	<li>TextString→マクロ実行時に挿入される文字列</li>
	<li>CompletionPrefix→マクロ発動文字列。例の場合、didselectと入力してCtrl+.を押すとマクロが実行される</li>
</ul>
他の項目はおおむね上の例のままで問題ない様子。<br />
<br />
ひとまず<br />
viewWillAppear, viewDidAppear, viewDidLoad, viewWillDisappear, viewDidDisappear, cellForRowAtIndexPath, didSelectRowAtIndexPathなどを登録してみた。なかなか悪くない。<br />
<br />
vwaと入力してCtrl+.でviewWillAppear発動。他もvda、vdl、vwd、vdd、cellfor、didselectで発動するのでいい感じ。<br />
<br />
この設定済みマクロはこちらから。<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/08&lt;/li&gt; &lt;p&gt;/ObjectiveC.xctxtmacro.zip">ObjectiveC.xctxtmacro</a><br />
<h3>参考</h3>
<ul>
	<li><a href="http://d.hatena.ne.jp/myomi/20090415/1239804053">nibファイルを使わないテンプレートを作る（１）Window-Based Application &#8211; ねこじたclockwork</a></li>
	<li><a href="http://d.hatena.ne.jp/griffin-stewie/20090315/p1">Xcodeのテンプレートを作成 &#8211; griffin-stewieの日記</a></li>
	<li><a href="http://www.turkeysheartrhinos.com/?p=8">Exending Xcode &#8211; File &amp; Project Templates, TextMate Style Macros | Turkeys ♥ Rhinos</a></li>
	<li><a href="http://www.mobileorchard.com/14-essential-xcode-tips-tricks-and-resources-for-iphone-devs/">14 Essential Xcode Tips, Tricks and Resources for iPhone Devs</a></li>
	<li><a href="http://shanesbrain.net/2008/7/9/using-xcode-with-git">*.pbxproj -crlf -diff -merge</a></li>
</ul></p>

	<p>タグ: <a href="http://iphone.longearth.net/tag/interface-builder/" title="Interface Builder" rel="tag nofollow">Interface Builder</a>, <a href="http://iphone.longearth.net/tag/iphone/" title="iphone" rel="tag nofollow">iphone</a>, <a href="http://iphone.longearth.net/tag/json/" title="JSON" rel="tag nofollow">JSON</a>, <a href="http://iphone.longearth.net/tag/mac/" title="mac" rel="tag nofollow">mac</a>, <a href="http://iphone.longearth.net/tag/objecti/" title="objecti" rel="tag nofollow">objecti</a>, <a href="http://iphone.longearth.net/tag/objective-c/" title="objective-c" rel="tag nofollow">objective-c</a>, <a href="http://iphone.longearth.net/tag/three20/" title="Three20" rel="tag nofollow">Three20</a>, <a href="http://iphone.longearth.net/tag/tips/" title="Tips" rel="tag nofollow">Tips</a>, <a href="http://iphone.longearth.net/tag/uitableview/" title="UITableView" rel="tag nofollow">UITableView</a>, <a href="http://iphone.longearth.net/tag/xcode/" title="Xcode" rel="tag nofollow">Xcode</a>, <a href="http://iphone.longearth.net/tag/%e3%82%a2%e3%83%97%e3%83%aa/" title="アプリ" rel="tag nofollow">アプリ</a>, <a href="http://iphone.longearth.net/tag/%e3%83%80%e3%82%a6%e3%83%b3%e3%83%ad%e3%83%bc%e3%83%89/" title="ダウンロード" rel="tag nofollow">ダウンロード</a>, <a href="http://iphone.longearth.net/tag/%e3%83%87%e3%83%90%e3%83%83%e3%82%b0/" title="デバッグ" rel="tag nofollow">デバッグ</a>, <a href="http://iphone.longearth.net/tag/%e3%83%aa%e3%83%aa%e3%83%bc%e3%82%b9/" title="リリース" rel="tag nofollow">リリース</a></p>

	<h4>関連する投稿</h4>
	<ul class="st-related-posts">
	<li><a href="http://iphone.longearth.net/2009/01/19/%e3%80%90uitableview%e3%80%91%e9%81%b8%e6%8a%9e%e8%a1%8c%e3%81%8b%e3%82%89%e5%88%a5%e7%94%bb%e9%9d%a2%e3%82%92%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%99/" title="【UITableView】選択行から別画面を呼び出す (2009 年 1 月 19 日)">【UITableView】選択行から別画面を呼び出す</a> </li>
	<li><a href="http://iphone.longearth.net/2009/01/20/%e3%80%90uitableview%e3%80%91uitableview%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e8%a9%b3%e7%b4%b0%e7%94%bb%e9%9d%a2%e3%82%92%e4%bd%9c%e3%82%8b-1%e3%80%90uitableviewdatasource%e3%80%91/" title="【UITableView】UITableViewを使って詳細画面を作る-1【UITableViewDataSource】 (2009 年 1 月 20 日)">【UITableView】UITableViewを使って詳細画面を作る-1【UITableViewDataSource】</a> </li>
	<li><a href="http://iphone.longearth.net/2008/12/31/%e3%80%90uitableview%e3%80%91uitableview%e3%81%ab%e6%96%87%e5%ad%97%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/" title="【UITableView】UITableViewに文字を表示する (2008 年 12 月 31 日)">【UITableView】UITableViewに文字を表示する</a> </li>
	<li><a href="http://iphone.longearth.net/2008/12/31/%e3%80%90uitableview%e3%80%91uitableview%e3%81%ab%e3%83%aa%e3%82%b9%e3%83%88%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/" title="【UITableView】UITableViewにリストを表示する (2008 年 12 月 31 日)">【UITableView】UITableViewにリストを表示する</a> </li>
	<li><a href="http://iphone.longearth.net/2009/03/09/%e3%80%90iphone%e3%80%91%e3%83%a1%e3%83%a2%e3%83%aa%e4%b8%8d%e8%b6%b3%e6%99%82%e3%81%ae%e3%82%b7%e3%83%9f%e3%83%a5%e3%83%ac%e3%83%bc%e3%83%88%e3%81%a8%e3%83%87%e3%83%90%e3%83%83%e3%82%b0/" title="【iPhone】メモリ不足時のシミュレートとデバッグ (2009 年 3 月 9 日)">【iPhone】メモリ不足時のシミュレートとデバッグ</a> </li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://iphone.longearth.net/2009/08/09/%e3%80%90xcode%e3%80%91%e8%a8%ad%e5%ae%9a%e3%81%97%e3%81%a6%e3%81%8a%e3%81%8f%e3%81%a8%e4%be%bf%e5%88%a9%e3%81%aa%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%9e%e3%82%a4%e3%82%ba%e3%81%84%e3%82%8d%e3%81%84/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>【UIVIewController】iPhoneを横向きにした時に別画面を表示する方法</title>
		<link>http://iphone.longearth.net/2009/04/07/%e3%80%90uiviewcontroller%e3%80%91iphone%e3%82%92%e6%a8%aa%e5%90%91%e3%81%8d%e3%81%ab%e3%81%97%e3%81%9f%e6%99%82%e3%81%ab%e5%88%a5%e7%94%bb%e9%9d%a2%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/</link>
		<comments>http://iphone.longearth.net/2009/04/07/%e3%80%90uiviewcontroller%e3%80%91iphone%e3%82%92%e6%a8%aa%e5%90%91%e3%81%8d%e3%81%ab%e3%81%97%e3%81%9f%e6%99%82%e3%81%ab%e5%88%a5%e7%94%bb%e9%9d%a2%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 15:31:26 +0000</pubDate>
		<dc:creator>daichi</dc:creator>
				<category><![CDATA[iphone開発]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPachi]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iSlot]]></category>
		<category><![CDATA[iSlot Pro]]></category>
		<category><![CDATA[objecti]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[UIViewController]]></category>
		<category><![CDATA[アプリ]]></category>
		<category><![CDATA[パチスロ]]></category>
		<category><![CDATA[リリース]]></category>
		<category><![CDATA[収支]]></category>
		<category><![CDATA[回転]]></category>
		<category><![CDATA[変換]]></category>
		<category><![CDATA[検知]]></category>
		<category><![CDATA[横向き]]></category>

		<guid isPermaLink="false">http://iphone.longearth.net/?p=474</guid>
		<description><![CDATA[
これだいぶやられました。
こんな風にただ横向きに画面を表示したいだけなのですが、ハマり倒しました。



二度とやられないためにもここに残します。


本体回転の検知
横向きにした時に別画面を表示するには、まず横向きを [...]]]></description>
			<content:encoded><![CDATA[<p>
これだいぶやられました。<br />
こんな風にただ横向きに画面を表示したいだけなのですが、ハマり倒しました。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-1.png"><img class="alignnone size-medium wp-image-478" title="横向き画面" src="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-1-300x161.png" alt="横向き画面" width="300" height="161" /></a><br />
<br />
二度とやられないためにもここに残します。<br />
<br />
<span id="more-474"></span><br />
<h3>本体回転の検知</h3>
横向きにした時に別画面を表示するには、まず横向きを検知する必要があります。<br />
UIViewControllerの以下のメソッドで本体の回転状態を検知できます。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p474code17'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p47417"><td class="code" id="p474code17"><pre class="objective-c" style="font-family:monospace;"> - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {</pre></td></tr></table></div>

<br />
<br />
このメソッドでNOを返せばViewの自動回転をしない、YESを返せば自動回転をする、という設定になります。<br />
が、加えて引数のinterfaceOrientationの状態を見れば本体がどの向きになっているのかを検知できます。<br />
<br />
UIInterfaceOrientationはenumで値はそれぞれ以下の状態を表します。<br />
<ul>
	<li>UIInterfaceOrientationPortrait</li>
これはホームボタンが画面の下側にある状態。通常の状態。<br />
	<li> UIInterfaceOrientationPortraitUpsideDown</li>
これはホームボタンが画面の上側にある状態。通常と逆さまの状態。<br />
	<li> UIInterfaceOrientationLandscapeLeft</li>
これはホームボタンが画面の左側にある状態。右に傾けた状態。<br />
	<li> UIInterfaceOrientationLandscapeRight</li>
これはホームボタンが画面の右側にある状態。左に傾けた状態。</ul>
本体を横向きにした場合に、別画面を表示するコードはこんな感じ。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p474code18'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p47418"><td class="code" id="p474code18"><pre class="objective-c" style="font-family:monospace;"> - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       //ホームボタンが下以外にある状態なら
 	if (UIInterfaceOrientationPortrait != interfaceOrientation){
&nbsp;
		iPachiAppDelegate *delegate = (iPachiAppDelegate *)[[UIApplication sharedApplication] delegate];
&nbsp;
               // Fade In Out のアニメーションで表示
		CATransition* animation;
		animation = [CATransition animation];
		animation.type = kCATransitionFade;
&nbsp;
		[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
&nbsp;
		GraffController *graff = [ControllerManager graffController];
		UITabBarController *tab = delegate.tabBarController;
&nbsp;
		[tab.view removeFromSuperview];
		UIView *win = delegate.window;
		[win addSubview:graff.view];
&nbsp;
		[win.layer addAnimation:animation forKey:nil];
&nbsp;
	}
    return NO;
}</pre></td></tr></table></div>

<br />
<br />
アニメーションでFade in out させて別画面を起動させています。<br />
最後にNOをreturnしているのは、呼び出し元画面を自動回転させないため。<br />
あくまで横向き時に別画面を呼び出すことにとどめています。<br />
<h3>横向き画面用に座標を変換</h3>
上の状態では画面を起動することはできますが、座標がホームボタンの左上になっているため、こんな画面になってしまいます。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-2.png"><img class="alignnone size-medium wp-image-480" title="横向きになりきれていない" src="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-2-300x161.png" alt="横向きになりきれていない" width="300" height="161" /></a><br />
<br />
背景画像も横向きサイズで用意したため、見事に縦に足りず、横にはみ出ています。<br />
<br />
まずはViewのframeを横向きサイズに変更します。<br />
ここでは呼び出し先のloadViewメソッドでframeを指定します。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p474code19'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p47419"><td class="code" id="p474code19"><pre class="objective-c" style="font-family:monospace;">- (void)loadView {
	CGRect frame = [[UIScreen mainScreen] applicationFrame];
	UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.height, frame.size.width)];
	view.autoresizesSubviews = YES;
	self.view = view;
	[view release];
}</pre></td></tr></table></div>

<br />
<br />
Interface Builderを使用している場合は、IB上でframeを横向きサイズに指定しておきます。<br />
<br />
これで呼び出し先のビューでdrawRectしている内容も横向きサイズ内で描画されるようになります。こんな感じ。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-3.png"><img class="alignnone size-medium wp-image-482" title="横向きフレーム補正" src="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-3-300x161.png" alt="横向きフレーム補正" width="300" height="161" /></a><br />
<br />
次に、左下になっているUIViewの原点を横向きにした時に左上にくるように座標を変換します。<br />
座標の変換にはCGAffineTransformを使います。<br />
<br />
コードからみてしまうとこんな感じ<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p474code20'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p47420"><td class="code" id="p474code20"><pre class="objective-c" style="font-family:monospace;">- (void)viewWillAppear:(BOOL)animated {
	CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 90 / 180.0f);
	transform = CGAffineTransformTranslate(transform, 80, 80);
	self.view.transform = transform;
}</pre></td></tr></table></div>

<br />
<br />
CGAffineTransformMakeRotationで引数にM_PI * 90 / 180.0fを与えていますが、こいつで座標を時計回りに90°回転させています。<br />
ちなみに90°回転させた状態はこんな感じになります。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-5.png"><img class="alignnone size-medium wp-image-486" title="90°回転" src="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-5-300x161.png" alt="90°回転" width="300" height="161" /></a><br />
<br />
どうも回転は描画前のcenterを中心に行われるようです。たぶん。<br />
<br />
次のCGAffineTransformTranslateで描画位置を移動させます。<br />
ここではx軸に80px,y軸に80px移動させます。<br />
x軸は右が、y軸は下が正の値です。<br />
<br />
これをすると左上にずれてしまっているviewが右下に移動してこの形になります。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-6.png"><img class="alignnone size-medium wp-image-487" title="横向き正常" src="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-6-300x161.png" alt="横向き正常" width="300" height="161" /></a><br />
<br />
ですが、このままでは反対の横に回転させるとこんな画面になってしまいます。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-7.png"><img class="alignnone size-medium wp-image-488" title="横向き反対" src="http://iphone.longearth.net/wp-content/uploads/2009/04/picture-7-300x161.png" alt="横向き反対" width="300" height="161" /></a><br />
<br />
今は、呼び出し元画面時に左右どちらに傾けようが、常に時計周りに座標を変換してしまっているためです。<br />
<br />
なので呼び出し元での傾ける方向を呼び出し先画面に渡して、その値によって傾ける方向を変えてやります。<br />
<br />
呼び出し元のコードは以下。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p474code21'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p47421"><td class="code" id="p474code21"><pre class="objective-c" style="font-family:monospace;"> - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       //ホームボタンが下以外にある状態なら
 	if (UIInterfaceOrientationPortrait != interfaceOrientation){
&nbsp;
		iPachiAppDelegate *delegate = (iPachiAppDelegate *)[[UIApplication sharedApplication] delegate];
&nbsp;
               // Fade In Out のアニメーションで表示
		CATransition* animation;
		animation = [CATransition animation];
		animation.type = kCATransitionFade;
&nbsp;
		[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
&nbsp;
		GraffController *graff = [ControllerManager graffController];
&nbsp;
　　　　 // 画面の向きを渡す
　　　　 graff.parentUIInterfaceOrientation = interfaceOrientation;
&nbsp;
		UITabBarController *tab = delegate.tabBarController;
&nbsp;
		[tab.view removeFromSuperview];
		UIView *win = delegate.window;
		[win addSubview:graff.view];
&nbsp;
		[win.layer addAnimation:animation forKey:nil];
&nbsp;
	}
    return NO;
}</pre></td></tr></table></div>

<br />
<br />
呼び出し先のコードは以下。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p474code22'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p47422"><td class="code" id="p474code22"><pre class="objective-c" style="font-family:monospace;">- (void)viewWillAppear:(BOOL)animated {
       // 右にホームボタンがある場合
	if (self.parentUIInterfaceOrientation == UIInterfaceOrientationLandscapeRight ){
　　　　// 90°時計回りに回転
		CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 90 / 180.0f);
		transform = CGAffineTransformTranslate(transform, 80, 80);
		self.view.transform = transform;	
&nbsp;
       // 左にホームボタンがある場合
	} else if (self.parentUIInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ){
　　　　// 90°半時計回りに回転
		CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI * 270 / 180.0f);
		transform = CGAffineTransformTranslate(transform, -80, -80);
		self.view.transform = transform;
	}
}</pre></td></tr></table></div>

<br />
<br />
呼び出し先にはこんな変数を定義しています。<br />
UIInterfaceOrientation parentUIInterfaceOrientation;<br />
<br />
ここまでで、横向き起動完了です。<br />
<br />
おつかれさまです。<br />
<br />
<h3>関連のあるアプリ</h3>
<div style="float:left; margin-right:10px; margin-bottom:10px">
<a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=307729455&amp;mt=8"><img src="http://iphone.longearth.net/wp-content/uploads/2008/12/islot_icon.png" alt="islot_icon" title="islot_icon" width="57" height="57" class="alignnone size-full wp-image-502" /></a><br />
</div>
<div style="float:left">
<a href="http://iphone.longearth.net/2009/03/14/%E3%80%90app-store%E3%80%91iphone%E3%81%A7%E6%A9%9F%E4%BC%9A%E5%89%B2%E7%AE%97%E5%87%BA%E3%81%8C%E3%81%A7%E3%81%8D%E3%82%8B%E3%83%91%E3%83%81%E3%82%B9%E3%83%AD%E5%8F%8E%E6%94%AF%E7%AE%A1%E7%90%86/">iSlot Pro</a> 2009/03/13 リリース<br />
ファイナンス　350円<br />
パチスロ収支管理アプリ<br />
</div>
<div style="float:right">
<a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=307729455&amp;mt=8"><img src="http://iphone.longearth.net/wp-content/uploads/2008/12/app_store_badge.png" alt="app_store_badge" title="app_store_badge" width="157" height="57" class="alignnone size-full wp-image-505" /></a><br />
</div>
<div style="clear:both"></div></p>

	<p>タグ: <a href="http://iphone.longearth.net/tag/interface-builder/" title="Interface Builder" rel="tag nofollow">Interface Builder</a>, <a href="http://iphone.longearth.net/tag/ipachi/" title="iPachi" rel="tag nofollow">iPachi</a>, <a href="http://iphone.longearth.net/tag/iphone/" title="iphone" rel="tag nofollow">iphone</a>, <a href="http://iphone.longearth.net/tag/islot/" title="iSlot" rel="tag nofollow">iSlot</a>, <a href="http://iphone.longearth.net/tag/islot-pro/" title="iSlot Pro" rel="tag nofollow">iSlot Pro</a>, <a href="http://iphone.longearth.net/tag/objecti/" title="objecti" rel="tag nofollow">objecti</a>, <a href="http://iphone.longearth.net/tag/objective-c/" title="objective-c" rel="tag nofollow">objective-c</a>, <a href="http://iphone.longearth.net/tag/uiviewcontroller/" title="UIViewController" rel="tag nofollow">UIViewController</a>, <a href="http://iphone.longearth.net/tag/%e3%82%a2%e3%83%97%e3%83%aa/" title="アプリ" rel="tag nofollow">アプリ</a>, <a href="http://iphone.longearth.net/tag/%e3%83%91%e3%83%81%e3%82%b9%e3%83%ad/" title="パチスロ" rel="tag nofollow">パチスロ</a>, <a href="http://iphone.longearth.net/tag/%e3%83%aa%e3%83%aa%e3%83%bc%e3%82%b9/" title="リリース" rel="tag nofollow">リリース</a>, <a href="http://iphone.longearth.net/tag/%e5%8f%8e%e6%94%af/" title="収支" rel="tag nofollow">収支</a>, <a href="http://iphone.longearth.net/tag/%e5%9b%9e%e8%bb%a2/" title="回転" rel="tag nofollow">回転</a>, <a href="http://iphone.longearth.net/tag/%e5%a4%89%e6%8f%9b/" title="変換" rel="tag nofollow">変換</a>, <a href="http://iphone.longearth.net/tag/%e6%a4%9c%e7%9f%a5/" title="検知" rel="tag nofollow">検知</a>, <a href="http://iphone.longearth.net/tag/%e6%a8%aa%e5%90%91%e3%81%8d/" title="横向き" rel="tag nofollow">横向き</a></p>

	<h4>関連する投稿</h4>
	<ul class="st-related-posts">
	<li><a href="http://iphone.longearth.net/2009/01/19/%e3%80%90uitableview%e3%80%91%e9%81%b8%e6%8a%9e%e8%a1%8c%e3%81%8b%e3%82%89%e5%88%a5%e7%94%bb%e9%9d%a2%e3%82%92%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%99/" title="【UITableView】選択行から別画面を呼び出す (2009 年 1 月 19 日)">【UITableView】選択行から別画面を呼び出す</a> </li>
	<li><a href="http://iphone.longearth.net/2009/01/20/%e3%80%90uitableview%e3%80%91uitableview%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e8%a9%b3%e7%b4%b0%e7%94%bb%e9%9d%a2%e3%82%92%e4%bd%9c%e3%82%8b-1%e3%80%90uitableviewdatasource%e3%80%91/" title="【UITableView】UITableViewを使って詳細画面を作る-1【UITableViewDataSource】 (2009 年 1 月 20 日)">【UITableView】UITableViewを使って詳細画面を作る-1【UITableViewDataSource】</a> </li>
	<li><a href="http://iphone.longearth.net/2009/02/26/%e3%80%90uitableview%e3%80%91grouped%e3%81%aa%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%ae%e3%82%b9%e3%82%af%e3%83%ad%e3%83%bc%e3%83%ab%e3%81%8c%e9%87%8d%e3%81%84/" title="【UITableView】Groupedなテーブルのスクロールが重い (2009 年 2 月 26 日)">【UITableView】Groupedなテーブルのスクロールが重い</a> </li>
	<li><a href="http://iphone.longearth.net/2009/03/09/%e3%80%90iphone%e3%80%91%e3%83%a1%e3%83%a2%e3%83%aa%e4%b8%8d%e8%b6%b3%e6%99%82%e3%81%ae%e3%82%b7%e3%83%9f%e3%83%a5%e3%83%ac%e3%83%bc%e3%83%88%e3%81%a8%e3%83%87%e3%83%90%e3%83%83%e3%82%b0/" title="【iPhone】メモリ不足時のシミュレートとデバッグ (2009 年 3 月 9 日)">【iPhone】メモリ不足時のシミュレートとデバッグ</a> </li>
	<li><a href="http://iphone.longearth.net/2009/05/09/%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92iphone-os-30%e3%81%b8%e5%af%be%e5%bf%9c%e3%81%95%e3%81%9b%e3%82%8b%e6%99%82%e3%81%8c%e3%81%8d%e3%81%be%e3%81%97%e3%81%9f/" title="アプリをiPhone OS 3.0へ対応させる時がきました (2009 年 5 月 9 日)">アプリをiPhone OS 3.0へ対応させる時がきました</a> </li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://iphone.longearth.net/2009/04/07/%e3%80%90uiviewcontroller%e3%80%91iphone%e3%82%92%e6%a8%aa%e5%90%91%e3%81%8d%e3%81%ab%e3%81%97%e3%81%9f%e6%99%82%e3%81%ab%e5%88%a5%e7%94%bb%e9%9d%a2%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>【UITableView】UITableViewを使って詳細画面を作る-1【UITableViewDataSource】</title>
		<link>http://iphone.longearth.net/2009/01/20/%e3%80%90uitableview%e3%80%91uitableview%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e8%a9%b3%e7%b4%b0%e7%94%bb%e9%9d%a2%e3%82%92%e4%bd%9c%e3%82%8b-1%e3%80%90uitableviewdatasource%e3%80%91/</link>
		<comments>http://iphone.longearth.net/2009/01/20/%e3%80%90uitableview%e3%80%91uitableview%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e8%a9%b3%e7%b4%b0%e7%94%bb%e9%9d%a2%e3%82%92%e4%bd%9c%e3%82%8b-1%e3%80%90uitableviewdatasource%e3%80%91/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 14:44:26 +0000</pubDate>
		<dc:creator>daichi</dc:creator>
				<category><![CDATA[iphone開発]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPachi]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[objecti]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[UIKit]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[UITableViewCell]]></category>
		<category><![CDATA[UITableViewDataSource]]></category>
		<category><![CDATA[UIViewController]]></category>
		<category><![CDATA[アプリ]]></category>
		<category><![CDATA[セル]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<category><![CDATA[デザイン]]></category>
		<category><![CDATA[パチスロ]]></category>
		<category><![CDATA[リスト]]></category>
		<category><![CDATA[リリース]]></category>
		<category><![CDATA[収支]]></category>

		<guid isPermaLink="false">http://iphone.longearth.net/?p=169</guid>
		<description><![CDATA[
前回まででUITableViewでリストを表示して、
行を選択すると別画面を表示するところまで
できました。

今回は行選択時にその行の詳細画面を表示する画面を
UITableViewを使って作ります。
学べること

 [...]]]></description>
			<content:encoded><![CDATA[<p>
前回まででUITableViewでリストを表示して、<br />
行を選択すると別画面を表示するところまで<br />
できました。<br />
<br />
今回は行選択時にその行の詳細画面を表示する画面を<br />
UITableViewを使って作ります。<br />
<h3>学べること</h3>
<ul>
	<li>コントローラ内でのデータをビューに表示する方法</li>
	<li>UITableViewに必要なもの-datasource</li>
</ul>
<span id="more-169"></span><br />
<h3>前提条件</h3>
<ul>
	<li><a href="http://iphone.longearth.net/?p=135&amp;preview=true">【UITableView】選択行から別画面を呼び出す</a>のプロジェクトの利用</li>
	<li><a href="http://iphone.longearth.net/?p=135&amp;preview=true">【UITableView】選択行から別画面を呼び出す</a>までの内容の理解</li>
</ul>
<h3>手順</h3>
<ul>
	<li>詳細画面のビュー作成</li>
	<li>詳細画面のコントローラ作成</li>
	<li>親画面から表示データを渡す</li>
	<li>ビルド</li>
</ul>
<h3>詳細画面のビュー作成</h3>
今回はサクサクいきます。<br />
前回までで真っ白なビューを作成しました。<br />
そこにUITableViewを追加します。<br />
<br />
Interface BuilderのLibraryの中からTableViewをドラッグして<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/tableviewe381aee981b8e68a9e.png"><img class="aligncenter size-medium wp-image-170" title="tableviewの選択" src="http://iphone.longearth.net/wp-content/uploads/2009/01/tableviewe381aee981b8e68a9e-127x300.png" alt="tableviewの選択" width="127" height="300" /></a><br />
<br />
viewのところまで持ってきます。<br />
Interface Builderは自動的にサイズがちょうどよい<br />
位置を微調整してくれるので、いいあんばいのところで<br />
ドラッグを離します。<br />
<br />
こんな感じになると思います。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/viewe381b8e381aetableviewe8bfbde58aa0.png"><img class="aligncenter size-medium wp-image-171" title="viewへのTableView追加" src="http://iphone.longearth.net/wp-content/uploads/2009/01/viewe381b8e381aetableviewe8bfbde58aa0-206x299.png" alt="viewへのTableView追加" width="206" height="299" /></a><br />
<br />
この時点でビルドして実行すると<br />
前回でビューとコントローラの接続が完了しているので、<br />
新画面に貼付けたTableViewが表示されるのを確認できます。<br />
<br />
ただ、まだ何も表示はされません。<br />
<br />
少し表示するデザインを詳細画面っぽく変えてみます。<br />
<br />
viewウィンドウのTable Viewを選択した後<br />
Inspectorウィンドウの一番左のタブAttributeを開き、<br />
Style項目を現在設定されているPlainからGroupedへ変更します。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/attribute-grouped.png"><img class="aligncenter size-medium wp-image-176" title="デザインをグループ形式に" src="http://iphone.longearth.net/wp-content/uploads/2009/01/attribute-grouped-116x300.png" alt="デザインをグループ形式に" width="116" height="300" /></a><br />
<br />
するとテーブルが囲まれたようなデザインへ変わります。<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/grouped-table.png"><img class="aligncenter size-medium wp-image-177" title="グループスタイルのTable View" src="http://iphone.longearth.net/wp-content/uploads/2009/01/grouped-table-206x299.png" alt="グループスタイルのTable View" width="206" height="299" /></a><br />
<br />
この時点でビルドしてもまだ表示する行数などが未設定なので<br />
背景しか表示されません。<br />
<h3>詳細画面のコントローラ作成</h3>
<h4>編集するファイル</h4>
<ul>
	<li>DetailViewController.h</li>
	<li>DetailViewController.m</li>
</ul>
次にコントローラの作り込みに入ります。<br />
<h4>ビュー上のTableViewとコントローラの紐付け</h4>
まずは先ほどビューに追加したUITableViewと<br />
コントローラを紐付けます。<br />
<br />
基本的にviewにUILableやUITextFieldといったコンポーネントを<br />
配置した場合、その内容をコントローラから制御するためには<br />
コントローラ内にビューに公開するフィールドを定義してやる<br />
必要があります。<br />
<br />
そのビューに公開するよという識別子はIBOutletです。<br />
通常のフィールド定義の前にIBOutletをつけるだけです。<br />
<br />
DetailController.hのファイルは以下のようになります。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p169code27'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p16927"><td class="code" id="p169code27"><pre class="objective-c" style="font-family:monospace;">#import 
&nbsp;
@interface DetailViewController : UIViewController{
	IBOutlet UITableView *myTableView;
}
&nbsp;
@property(nonatomic, retain) UITableView *myTableView;
&nbsp;
@end</pre></td></tr></table></div>

<br />
<br />
getter/setterを用意するためにDetailViewController.mにも<br />
以下の記述を追加します。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p169code28'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p16928"><td class="code" id="p169code28"><pre class="objective-c" style="font-family:monospace;">@synthesize myTableView;</pre></td></tr></table></div>

<br />
<br />
以上の定義でビューからIBOutlet付きのフィールド<br />
myTableViewが見えるようになります。<br />
<br />
Interface Builderに戻って、File&#8217;s Ownerオブジェクトを右クリック<br />
してみてください。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/e585ace9968be38195e3828ce3819fmytableview.png"><img class="aligncenter size-medium wp-image-185" title="公開されたmyTableView" src="http://iphone.longearth.net/wp-content/uploads/2009/01/e585ace9968be38195e3828ce3819fmytableview-300x160.png" alt="公開されたmyTableView" width="300" height="160" /></a><br />
<br />
こんな感じでmyTableViewがOutletsの中に表示されています。<br />
<br />
試しにIBOutletを外して保存してみると、<br />
表示されなくなることが分かると思います。<br />
<br />
次にこのmyTableViewをビュー上のUITableViewと紐づけます。<br />
<br />
紐付けは前回と同じように、myTableViewの右の○をビュー上の<br />
UITableViewの位置までドラッグするだけです。<br />
<br />
すると以下のように紐付けがされます。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/mytableviewe381a8e38393e383a5e383bce4b88ae381aetableviewe381a8e381aee7b490e4bb98e38191.png"><img class="aligncenter size-medium wp-image-186" title="myTableViewとビュー上のTableVIewの紐付け" src="http://iphone.longearth.net/wp-content/uploads/2009/01/mytableviewe381a8e38393e383a5e383bce4b88ae381aetableviewe381a8e381aee7b490e4bb98e38191-300x160.png" alt="myTableViewとビュー上のTableVIewの紐付け" width="300" height="160" /></a><br />
<h4>表示するTableViewの設定</h4>
ビューとの連携が済んだので<br />
詳細画面に表示する行数を指定しましょう。<br />
<br />
行数は詳細画面に何を表示するかによりますが、<br />
ここではそうですね、パチスロの収支データの詳細表示をするとして<br />
日付、機種、金額、メモの４項目を表示することにします。<br />
<br />
UITableViewに表示する行数は<a href="http://iphone.longearth.net/2008/12/31/%e3%80%90uitableview%e3%80%91uitableview%e3%81%ab%e6%96%87%e5%ad%97%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/">【UITableView】UITableViewに文字を表示する</a>で見たように<br />
numberOfRowsInSection:メソッドで指定します。<br />
<br />
なので以下のようなコードを追加します。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p169code29'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p16929"><td class="code" id="p169code29"><pre class="objective-c" style="font-family:monospace;">- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return 4;
}</pre></td></tr></table></div>

<br />
<br />
実はこの段階ではまだ４行のテーブルは表示されません。<br />
行数が指定されていても、その行に表示するセルが未設定です。<br />
<br />
というわけでセルを表示するために<br />
– tableView:cellForRowAtIndexPath:メソッドを追加します。<br />
これも<a href="http://iphone.longearth.net/2008/12/31/%e3%80%90uitableview%e3%80%91uitableview%e3%81%ab%e6%96%87%e5%ad%97%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/">【UITableView】UITableViewに文字を表示する</a>で出ましたね。<br />
<br />
追加するのは空のセルを表示する以下のコードです。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p169code30'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p16930"><td class="code" id="p169code30"><pre class="objective-c" style="font-family:monospace;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
&nbsp;
    static NSString *CellIdentifier = @&quot;Cell&quot;;
&nbsp;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
&nbsp;
	// Set up the cell...
    return cell;
}</pre></td></tr></table></div>

<br />
<br />
実はこの段階でも４行のテーブルは表示されません。<br />
<br />
というのもこれらのメソッドはUITableViewDataSourceプロトコルで<br />
定義されているメソッドだからです。<br />
<br />
UITableViewでデータを表示するには<br />
UITableViewDataSourceの役目を誰かにしてもらう必要があります。<br />
その誰かというのはこのDetailViewControllerで今行おうとしているのですが、<br />
TableViewからしてみればまだ、誰がDataSource役をしてくれるのかの判断が<br />
できない状態です。<br />
<br />
というわけで例によってInterface BuilderでDataSource役を指定します。<br />
ビュー上のUITableViewを選択して右クリックすると以下のような<br />
UITableViewのOutletsが表示されます。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/tableviewe381aeoutlets.png"><img class="aligncenter size-medium wp-image-187" title="tableviewのoutlets" src="http://iphone.longearth.net/wp-content/uploads/2009/01/tableviewe381aeoutlets-300x147.png" alt="tableviewのoutlets" width="300" height="147" /></a><br />
<br />
その中のDataSource項目をDetailViewControllerのことを指しているFile&#8217;s Ownerへ紐づけます。<br />
<br />
こんなふうになるはずです。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/datasourcee381aee7b490e4bb98e38191.png"><img class="aligncenter size-medium wp-image-188" title="datasourceの紐付け" src="http://iphone.longearth.net/wp-content/uploads/2009/01/datasourcee381aee7b490e4bb98e38191-300x147.png" alt="datasourceの紐付け" width="300" height="147" /></a><br />
<br />
ここまできたらいったんビルドをしてみましょう。<br />
<br />
コード内で指定したように４行の空セルが表示されますね。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/e7a9bae381aeefbc94e8a18ce38386e383bce38396e383ab.png"><img class="aligncenter size-medium wp-image-193" title="空の４行テーブル" src="http://iphone.longearth.net/wp-content/uploads/2009/01/e7a9bae381aeefbc94e8a18ce38386e383bce38396e383ab-161x300.png" alt="空の４行テーブル" width="161" height="300" /></a><br />
<br />
ここまでで分かるように、UITableVIewでデータを表示するために最低限<br />
必要なものはDataSourceの指定と<br />
– tableView:numberOfRowsInSection:<br />
– tableView:cellForRowAtIndexPath:<br />
の２つのメソッドです。<br />
<br />
ここではTableVIewDataSourceはFile&#8217;s OwnerであるDetailViewControllerに設定していますが、<br />
当然UITableViewDataSourceプロトコルを実装したクラスなら<br />
他のクラスでも問題はありません。<br />
<br />
UITableVIewDataSourceプロトコルの詳細はやはり本家が参考になります。<br />
<a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDataSource">– tableView:numberOfRowsInSection:</a><br />
<br />
ちょっと長めになってきたので、ここで一旦切ります。<br />
<br />
<h3>関連のあるアプリ</h3>
<div style="float:left; margin-right:10px; margin-bottom:10px">
<a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=304074830&amp;mt=8"><img src="http://iphone.longearth.net/wp-content/uploads/2008/12/ipachi2_icon.png" alt="ipachi2_icon" title="ipachi2_icon" width="57" height="57" class="alignleft size-full wp-image-503" /></a><br />
</div>
<div style="float:left">
<a href="http://iphone.longearth.net/2009/02/10/ipachi%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/"><span style="font-size:14px;">iPachi</span></a> 2009/02/10 リリース<br />
ファイナンス　無料<br />
簡易版パチンコパチスロ収支管理アプリ<br />
</div>
<div style="float:right">
<a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=304074830&amp;mt=8"><img src="http://iphone.longearth.net/wp-content/uploads/2008/12/app_store_badge.png" alt="app_store_badge" title="app_store_badge" width="157" height="57" class="alignnone size-full wp-image-505" /></a><br />
</div>
<div style="clear:both"></div></p>

	<p>タグ: <a href="http://iphone.longearth.net/tag/interface-builder/" title="Interface Builder" rel="tag nofollow">Interface Builder</a>, <a href="http://iphone.longearth.net/tag/ipachi/" title="iPachi" rel="tag nofollow">iPachi</a>, <a href="http://iphone.longearth.net/tag/iphone/" title="iphone" rel="tag nofollow">iphone</a>, <a href="http://iphone.longearth.net/tag/nsstring/" title="NSString" rel="tag nofollow">NSString</a>, <a href="http://iphone.longearth.net/tag/objecti/" title="objecti" rel="tag nofollow">objecti</a>, <a href="http://iphone.longearth.net/tag/objective-c/" title="objective-c" rel="tag nofollow">objective-c</a>, <a href="http://iphone.longearth.net/tag/uikit/" title="UIKit" rel="tag nofollow">UIKit</a>, <a href="http://iphone.longearth.net/tag/uitableview/" title="UITableView" rel="tag nofollow">UITableView</a>, <a href="http://iphone.longearth.net/tag/uitableviewcell/" title="UITableViewCell" rel="tag nofollow">UITableViewCell</a>, <a href="http://iphone.longearth.net/tag/uitableviewdatasource/" title="UITableViewDataSource" rel="tag nofollow">UITableViewDataSource</a>, <a href="http://iphone.longearth.net/tag/uiviewcontroller/" title="UIViewController" rel="tag nofollow">UIViewController</a>, <a href="http://iphone.longearth.net/tag/%e3%82%a2%e3%83%97%e3%83%aa/" title="アプリ" rel="tag nofollow">アプリ</a>, <a href="http://iphone.longearth.net/tag/%e3%82%bb%e3%83%ab/" title="セル" rel="tag nofollow">セル</a>, <a href="http://iphone.longearth.net/tag/%e3%83%81%e3%83%a5%e3%83%bc%e3%83%88%e3%83%aa%e3%82%a2%e3%83%ab/" title="チュートリアル" rel="tag nofollow">チュートリアル</a>, <a href="http://iphone.longearth.net/tag/%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3/" title="デザイン" rel="tag nofollow">デザイン</a>, <a href="http://iphone.longearth.net/tag/%e3%83%91%e3%83%81%e3%82%b9%e3%83%ad/" title="パチスロ" rel="tag nofollow">パチスロ</a>, <a href="http://iphone.longearth.net/tag/%e3%83%aa%e3%82%b9%e3%83%88/" title="リスト" rel="tag nofollow">リスト</a>, <a href="http://iphone.longearth.net/tag/%e3%83%aa%e3%83%aa%e3%83%bc%e3%82%b9/" title="リリース" rel="tag nofollow">リリース</a>, <a href="http://iphone.longearth.net/tag/%e5%8f%8e%e6%94%af/" title="収支" rel="tag nofollow">収支</a></p>

	<h4>関連する投稿</h4>
	<ul class="st-related-posts">
	<li><a href="http://iphone.longearth.net/2009/01/19/%e3%80%90uitableview%e3%80%91%e9%81%b8%e6%8a%9e%e8%a1%8c%e3%81%8b%e3%82%89%e5%88%a5%e7%94%bb%e9%9d%a2%e3%82%92%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%99/" title="【UITableView】選択行から別画面を呼び出す (2009 年 1 月 19 日)">【UITableView】選択行から別画面を呼び出す</a> </li>
	<li><a href="http://iphone.longearth.net/2008/12/31/%e3%80%90uitableview%e3%80%91uitableview%e3%81%ab%e3%83%aa%e3%82%b9%e3%83%88%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/" title="【UITableView】UITableViewにリストを表示する (2008 年 12 月 31 日)">【UITableView】UITableViewにリストを表示する</a> </li>
	<li><a href="http://iphone.longearth.net/2008/12/31/%e3%80%90uitableview%e3%80%91uitableview%e3%81%ab%e6%96%87%e5%ad%97%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/" title="【UITableView】UITableViewに文字を表示する (2008 年 12 月 31 日)">【UITableView】UITableViewに文字を表示する</a> </li>
	<li><a href="http://iphone.longearth.net/2009/02/25/%e3%80%90uitableviewcell%e3%80%91%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0cell%e3%81%8c%e7%b7%a8%e9%9b%86%e4%b8%ad%e3%81%ab%e7%94%bb%e9%9d%a2%e5%a4%96%e3%81%ab%e3%81%af%e3%81%bf%e5%87%ba%e3%82%8b/" title="【UITableViewCell】カスタムCellが編集中に画面外にはみ出る (2009 年 2 月 25 日)">【UITableViewCell】カスタムCellが編集中に画面外にはみ出る</a> </li>
	<li><a href="http://iphone.longearth.net/2009/04/07/%e3%80%90uiviewcontroller%e3%80%91iphone%e3%82%92%e6%a8%aa%e5%90%91%e3%81%8d%e3%81%ab%e3%81%97%e3%81%9f%e6%99%82%e3%81%ab%e5%88%a5%e7%94%bb%e9%9d%a2%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/" title="【UIVIewController】iPhoneを横向きにした時に別画面を表示する方法 (2009 年 4 月 7 日)">【UIVIewController】iPhoneを横向きにした時に別画面を表示する方法</a> </li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://iphone.longearth.net/2009/01/20/%e3%80%90uitableview%e3%80%91uitableview%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e8%a9%b3%e7%b4%b0%e7%94%bb%e9%9d%a2%e3%82%92%e4%bd%9c%e3%82%8b-1%e3%80%90uitableviewdatasource%e3%80%91/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>【UITableView】選択行から別画面を呼び出す</title>
		<link>http://iphone.longearth.net/2009/01/19/%e3%80%90uitableview%e3%80%91%e9%81%b8%e6%8a%9e%e8%a1%8c%e3%81%8b%e3%82%89%e5%88%a5%e7%94%bb%e9%9d%a2%e3%82%92%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%99/</link>
		<comments>http://iphone.longearth.net/2009/01/19/%e3%80%90uitableview%e3%80%91%e9%81%b8%e6%8a%9e%e8%a1%8c%e3%81%8b%e3%82%89%e5%88%a5%e7%94%bb%e9%9d%a2%e3%82%92%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%99/#comments</comments>
		<pubDate>Mon, 19 Jan 2009 13:31:03 +0000</pubDate>
		<dc:creator>daichi</dc:creator>
				<category><![CDATA[iphone開発]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPachi]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[Navigation Controller]]></category>
		<category><![CDATA[objecti]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[UIViewController]]></category>
		<category><![CDATA[Xcode]]></category>
		<category><![CDATA[アイコン]]></category>
		<category><![CDATA[アプリ]]></category>
		<category><![CDATA[チュートリアル]]></category>
		<category><![CDATA[パチスロ]]></category>
		<category><![CDATA[リスト]]></category>
		<category><![CDATA[リリース]]></category>
		<category><![CDATA[動画]]></category>
		<category><![CDATA[収支]]></category>
		<category><![CDATA[画面遷移]]></category>

		<guid isPermaLink="false">http://iphone.longearth.net/?p=135</guid>
		<description><![CDATA[
今回はUITableVIewの行をタップした時に別画面を呼び出す
部分を作ります。

行をタップした時にプログラム内部ではUITableViewDelegateで定義されている
tableView:didSelectR [...]]]></description>
			<content:encoded><![CDATA[<p>
今回はUITableVIewの行をタップした時に別画面を呼び出す<br />
部分を作ります。<br />
<br />
行をタップした時にプログラム内部ではUITableViewDelegateで定義されている<br />
tableView:didSelectRowAtIndexPath:メソッドが呼ばれます。<br />
<br />
なので、このメソッド内に別画面を呼び出す処理を書いていきます。<br />
<h3>学べること</h3>
<ul>
	<li>Interface Builderの基本的な使い方</li>
	<li>UITableViewの行選択時の動作</li>
	<li>navigation Controllerによる画面遷移</li>
</ul>
<span id="more-135"></span><br />
<h3>前提条件</h3>
<ul>
	<li> <a title="Permanent Link to 【UITableView】UITableViewにリストを表示する" rel="bookmark" href="../2008/12/31/%e3%80%90uitableview%e3%80%91uitableview%e3%81%ab%e3%83%aa%e3%82%b9%e3%83%88%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/">【UITableView】UITableViewにリストを表示する</a>のプロジェクトを使用</li>
</ul>
<h3>手順</h3>
<ol>
	<li>新規画面のビュー作成</li>
	<li>新規画面のコントローラ作成</li>
	<li>ビューとコントローラの関連づけ</li>
	<li>親画面から新規画面の呼び出し</li>
	<li>ビルド</li>
</ol>
<h3>新規画面のビュー作成</h3>
行をタップした時に別画面を呼び出す処理を書こうにも<br />
呼び出す画面がなければ書きようがないので、<br />
まずは画面を用意します。<br />
<br />
iphoneアプリ開発ではMVCモデルと呼ばれるパターンが使われていますが、<br />
画面はMVCのうちのV、ビューにあたります。<br />
<br />
ビューはコードをゴリゴリ書いて作り上げることもできますが、<br />
Interface Builderを使うと見た目を確認しながら感覚的に作ることが<br />
できるので、こちらの方法で作成することにします。<br />
<h4>nibファイルの作成</h4>
Interface Builderで作成するファイルはnibファイルと呼ばれますが、<br />
まずはこれから作ります。<br />
<br />
Xcodeの左のツリーからResoucesを選択した状態で<br />
右クリック→追加→新規ファイルとしてファイル追加ウィンドウを表示します。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/e696b0e8a68fe38395e382a1e382a4e383abe8bfbde58aa0.png"><img class="aligncenter size-medium wp-image-139" title="新規ビューファイルの追加" src="http://iphone.longearth.net/wp-content/uploads/2009/01/e696b0e8a68fe38395e382a1e382a4e383abe8bfbde58aa0-300x188.png" alt="新規ビューファイルの追加" width="300" height="188" /></a><br />
<br />
新規ファイルとしてVIEW XIBを選択して、次へを押します。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/viewxibe38292e981b8e68a9e.png"><img class="aligncenter size-medium wp-image-140" title="VIEW XIBを選択" src="http://iphone.longearth.net/wp-content/uploads/2009/01/viewxibe38292e981b8e68a9e-300x230.png" alt="VIEW XIBを選択" width="300" height="230" /></a><br />
<br />
ファイル名を入力します。ここではDetailView.xibとします。<br />
次に完了を。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/e38393e383a5e383bce381aee38395e382a1e382a4e383abe5908de38292e585a5e58a9b.png"><img class="aligncenter size-medium wp-image-141" title="ビューのファイル名を入力" src="http://iphone.longearth.net/wp-content/uploads/2009/01/e38393e383a5e383bce381aee38395e382a1e382a4e383abe5908de38292e585a5e58a9b-300x230.png" alt="ビューのファイル名を入力" width="300" height="230" /></a><br />
<br />
左のツリーのNIBファイルをクリックするとDetailView.xibが作成されているのが確認できます。<br />
次に画面上部のファイルリストからDetailView.xibをダブルクリックして<br />
Inteface Builderを起動します。<br />
<br />
起動するとこんなウィンドウが出ます。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/detailviewxibe381aee4b8ade8baab.png"><img class="aligncenter size-medium wp-image-144" title="DetailView.xibの中身" src="http://iphone.longearth.net/wp-content/uploads/2009/01/detailviewxibe381aee4b8ade8baab-300x285.png" alt="DetailView.xibの中身" width="300" height="285" /></a><br />
<br />
これがnibファイルの中身で、３つのオブジェクトが<br />
含まれています。<br />
<br />
もう一つviewという真っ白なウィンドウが表示されます。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/view.png"><img class="aligncenter size-medium wp-image-145" title="view" src="http://iphone.longearth.net/wp-content/uploads/2009/01/view-206x299.png" alt="view" width="206" height="299" /></a><br />
<br />
これがiPhoneで表示される画面です。<br />
ここにいろんなパーツを追加していきます。<br />
<br />
パーツの追加はLibraryから行います。<br />
Libraryはこんなウィンドウです。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/library.png"><img class="aligncenter size-medium wp-image-146" title="library" src="http://iphone.longearth.net/wp-content/uploads/2009/01/library-127x300.png" alt="library" width="127" height="300" /></a><br />
<br />
Libraryの外観は左下の設定ボタンで<br />
アイコンのみを表示など変更ができます。<br />
<br />
Libraryが起動していない場合、<br />
上部のメニュー→Tools→Libraryとして起動します。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/librarye381aee8b5b7e58b95.png"><img class="aligncenter size-medium wp-image-147" title="libraryの起動" src="http://iphone.longearth.net/wp-content/uploads/2009/01/librarye381aee8b5b7e58b95-300x157.png" alt="libraryの起動" width="300" height="157" /></a><br />
<br />
あとはオブジェクト同士の接続や、表示設定をするための<br />
Inspectorウィンドウがあります。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/connections-inspector.png"><img class="aligncenter size-medium wp-image-149" title="connections-inspector" src="http://iphone.longearth.net/wp-content/uploads/2009/01/connections-inspector-116x300.png" alt="connections-inspector" width="116" height="300" /></a><br />
<br />
起動していない場合は、<br />
メニュー→Tools→Inspectorとして起動します。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/connections-inspectore381aee8b5b7e58b95.png"><img class="aligncenter size-medium wp-image-148" title="connections inspectoreの起動" src="http://iphone.longearth.net/wp-content/uploads/2009/01/connections-inspectore381aee8b5b7e58b95-300x155.png" alt="connections inspectoreの起動" width="300" height="155" /></a><br />
<br />
今回はこの空っぽのつまらない画面を<br />
表示するのにとどめます。<br />
<br />
一度にいろいろやりすぎると<br />
どの操作がどの結果につながっているのかが<br />
わかりにくくなるので。<br />
<h3>新規画面のコントローラ作成</h3>
というわけで、次にビューに紐づく<br />
コントローラを作成します。<br />
<br />
コントローラはモデルとビューとの橋渡し役です。<br />
画面上での操作をコントローラが受けて<br />
モデルへ処理をさせたり、別のビューを表示したり<br />
するのがコントローラの役目です。<br />
<br />
左のツリーClassesを選択して<br />
右クリックから→新規→新規ファイル追加として<br />
ファイル追加ウィンドウを表示します。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/e382b3e383b3e38388e383ade383bce383a9e381aee8bfbde58aa0.png"><img class="aligncenter size-medium wp-image-151" title="コントローラの追加" src="http://iphone.longearth.net/wp-content/uploads/2009/01/e382b3e383b3e38388e383ade383bce383a9e381aee8bfbde58aa0-300x187.png" alt="コントローラの追加" width="300" height="187" /></a><br />
<br />
Cocoa Touch Classesの中からUIViewController subclassを選択して次へ。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/e38386e383b3e38397e383ace383bce38388e381a7e382b3e383b3e38388e383ade383bce383a9e38292e981b8e68a9e.png"><img class="aligncenter size-medium wp-image-152" title="テンプレートでコントローラを選択" src="http://iphone.longearth.net/wp-content/uploads/2009/01/e38386e383b3e38397e383ace383bce38388e381a7e382b3e383b3e38388e383ade383bce383a9e38292e981b8e68a9e-300x230.png" alt="テンプレートでコントローラを選択" width="300" height="230" /></a><br />
<br />
ファイル名はDetailViewController.mとします。<br />
このとき、同時に&#8221;DetailViewController.h&#8221;も作成にチェックを忘れずに。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/detailviewcontrollere4bd9ce68890.png"><img class="aligncenter size-medium wp-image-153" title="detailviewcontrollerの作成" src="http://iphone.longearth.net/wp-content/uploads/2009/01/detailviewcontrollere4bd9ce68890-300x230.png" alt="detailviewcontrollerの作成" width="300" height="230" /></a><br />
<br />
以上でコントローラが作成されましたね。<br />
<br />
次にこのコントローラとビューとを紐づけます。<br />
<h3>ビューとコントローラの関連づけ</h3>
ビュー、コントローラ、それぞれの作成は済みましたが<br />
これらは何の関連もまだ持っていません。<br />
<br />
ビューを管理をするコントローラを紐付けます。<br />
<h4>File&#8217;s Ownerとコントローラの紐付け</h4>
まずDetailView.xibウィンドウのFile&#8217;s Ownerオブジェクトを選択して<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/files-ownere38292e981b8e68a9e.png"><img class="aligncenter size-medium wp-image-154" title="files-ownerを選択" src="http://iphone.longearth.net/wp-content/uploads/2009/01/files-ownere38292e981b8e68a9e-300x285.png" alt="files-ownerを選択" width="300" height="285" /></a><br />
<br />
Inspectorウィンドウの一番右のタブIdentityを選択して<br />
一番上のClass Identityプルダウンで先ほど作成したDetailViewControllerを<br />
選択します。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/identitye381a7e382afe383a9e382b9e38292e68c87e5ae9a.png"><img class="aligncenter size-medium wp-image-155" title="identityでクラス名を指定" src="http://iphone.longearth.net/wp-content/uploads/2009/01/identitye381a7e382afe383a9e382b9e38292e68c87e5ae9a-116x300.png" alt="identityでクラス名を指定" width="116" height="300" /></a><br />
<br />
これでDetailView.xibの所有者は<br />
DetailViewControllerだという設定がされました。<br />
<h4>ビューとFile&#8217;s Ownerの紐付け</h4>
次はビューとFile&#8217;s Ownerを関連づけます。<br />
File&#8217;s Ownerを右クリックすると<br />
黒いウィンドウが表示されます。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/e38393e383a5e383bce381aee7b490e4bb98e38191.png"><img class="aligncenter size-medium wp-image-157" title="ビューの紐付け" src="http://iphone.longearth.net/wp-content/uploads/2009/01/e38393e383a5e383bce381aee7b490e4bb98e38191-300x216.png" alt="ビューの紐付け" width="300" height="216" /></a><br />
<br />
このウィンドウ内のOutletsの中にviewという項目があるので、<br />
その項目の右の○をドラッグして、viewウィンドウまで<br />
引っ張ってドラッグをはなします。<br />
<br />
するとこんな感じでview項目にドラッグ先のviewが紐づきます。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/e7b490e4bb98e38191e5ae8ce4ba86.png"><img class="aligncenter size-medium wp-image-158" title="紐付け完了" src="http://iphone.longearth.net/wp-content/uploads/2009/01/e7b490e4bb98e38191e5ae8ce4ba86-300x147.png" alt="紐付け完了" width="300" height="147" /></a><br />
<br />
File&#8217;s Ownerとコントローラを紐付け、<br />
File&#8217;s Ownerとビューを紐付けたので、<br />
これでコントローラとビューの紐付けが完了しました。<br />
<br />
新画面の準備はこれで全部完了です。<br />
<br />
Interface Builder上でCommand+Sとして<br />
内容を保存しておきましょう。<br />
<h3>親画面から新規画面の呼び出し</h3>
<h4>編集するファイル</h4>
<ul>
	<li>RootViewController.m</li>
</ul>
やっと新画面の準備が終わったので<br />
いよいよ新画面の呼び出し処理を書きます。<br />
<br />
冒頭でも説明した通り、UITableViewで選択行をタップすると<br />
tableView:didSelectRowAtIndexPath:メソッドが呼ばれます。<br />
<br />
ここに呼び出す処理を書きます。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p135code33'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p13533"><td class="code" id="p135code33"><pre class="objective-c" style="font-family:monospace;">- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@&quot;DetailView&quot; bundle:nil];
	[self.navigationController pushViewController:detailViewController animated:YES];
	[detailViewController release];
}</pre></td></tr></table></div>

<br />
<br />
まず先ほど作成したDetailViewControllerのインスタンスを作成します。<br />
[DetailViewController alloc]<br />
<br />
作成したインスタンスのinitWithNibName:bundleメソッドを使って初期化します。<br />
initWithName:に指定する名前はビューの名前と同じにする必要があります。<br />
ここではDetailView.xibを作成したので、名前にDetailViewを指定しています。<br />
<br />
bundleはひとまずnilで問題ありません。<br />
<br />
次に作成したコントローラのインスタンスをnavigationコントローラの<br />
スタックに追加します。<br />
<br />
navigationコントローラはスタックに積まれた一番上のコントローラを表示制御<br />
に選択するので、プッシュされたコントローラに対応する画面が表示されます。<br />
<br />
最後にプッシュされたDetailViewControllerのインスタンスは<br />
navigationコントローラ内で参照カウンタが１つ増やされるので、<br />
こちらで作成したインスタンスはreleaseして参照カウンタを１つ戻します。<br />
<br />
javaやC#といったガベージコレクションをしてくれる<br />
言語を使っていた僕にとってはこのreleaseで参照を解放してしまう<br />
という理屈がはじめ理解できなくてハマりましたが、<br />
最近やっとObjective-Cのメモリの管理方法を理解してきて<br />
分かるようになりました。<br />
<br />
参照カウンタ系の話はまた別で書きます。<br />
<br />
これだけだと、RootViewController.mはDetailViewControllerという<br />
クラスがどれなのか判断がつかないので、ファイルの頭で<br />
以下のようにDetailViewController.hをimportしておきます。<br />
<br />

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p135code34'); return false;">View Code</a> OBJECTIVE-C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p13534"><td class="code" id="p135code34"><pre class="objective-c" style="font-family:monospace;">#import &quot;RootViewController.h&quot;
#import &quot;HelloWorldAppDelegate.h&quot;
#import &quot;DetailViewController.h&quot;
&nbsp;
@implementation RootViewController</pre></td></tr></table></div>

<br />
<br />
以上でコーディングは終了です。<br />
<h3>ビルド</h3>
それではビルドしてみましょう。<br />
<br />
起動画面はこれまで作成したリストが表示されています。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/e8b5b7e58b95e794bbe99da2.png"><img class="aligncenter size-medium wp-image-161" title="起動画面" src="http://iphone.longearth.net/wp-content/uploads/2009/01/e8b5b7e58b95e794bbe99da2-161x300.png" alt="起動画面" width="161" height="300" /></a><br />
<br />
ここで行を選択すると下のような新しい画面が表示されます。<br />
<br />
<a href="http://iphone.longearth.net/wp-content/uploads/2009/01/e8bfbde58aa0e794bbe99da21.png"><img class="aligncenter size-medium wp-image-163" title="追加画面" src="http://iphone.longearth.net/wp-content/uploads/2009/01/e8bfbde58aa0e794bbe99da21-161x300.png" alt="追加画面" width="161" height="300" /></a><br />
<br />
でもちょっと待ってください。<br />
さっきは空っぽのビューを用意しただけなのに、<br />
画面上部にナビゲーションバーが表示されていますね。<br />
<br />
この部分はnavigationコントローラが作っています。<br />
navigationControllerのスタックに新画面をプッシュしたので、<br />
新画面は始めの画面の小画面だよとnavigationControllerが<br />
認識していて、画面上部に自動的に親画面へ戻るボタンを<br />
表示してくれているわけですね。<br />
<br />
同時にいろいろやりすぎるとこのような基本的な<br />
部分の処理がどこで行われているのかが<br />
見えなくなってしまうので今回は<br />
あえて何も表示しないビューを作成しました。<br />
<br />
選択行から別画面を呼び出すは以上です。<br />
<br />
ちなみにnavigationControllerの<br />
pushViewController:animatedメソッドの<br />
animatedにYESを渡しているので<br />
いかにもiPhoneらしいみよ〜んという<br />
アニメーションで新画面を表示していますが、<br />
animated:NOとするとニヒルにシュッと新画面を<br />
表示できます。<br />
<br />
<h3>関連のあるアプリ</h3>
<div style="float:left; margin-right:10px; margin-bottom:10px">
<a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=304074830&amp;mt=8"><img src="http://iphone.longearth.net/wp-content/uploads/2008/12/ipachi2_icon.png" alt="ipachi2_icon" title="ipachi2_icon" width="57" height="57" class="alignleft size-full wp-image-503" /></a><br />
</div>
<div style="float:left">
<a href="http://iphone.longearth.net/2009/02/10/ipachi%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/"><span style="font-size:14px;">iPachi</span></a> 2009/02/10 リリース<br />
ファイナンス　無料<br />
簡易版パチンコパチスロ収支管理アプリ<br />
</div>
<div style="float:right">
<a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=304074830&amp;mt=8"><img src="http://iphone.longearth.net/wp-content/uploads/2008/12/app_store_badge.png" alt="app_store_badge" title="app_store_badge" width="157" height="57" class="alignnone size-full wp-image-505" /></a><br />
</div>
<div style="clear:both"></div></p>

	<p>タグ: <a href="http://iphone.longearth.net/tag/interface-builder/" title="Interface Builder" rel="tag nofollow">Interface Builder</a>, <a href="http://iphone.longearth.net/tag/ipachi/" title="iPachi" rel="tag nofollow">iPachi</a>, <a href="http://iphone.longearth.net/tag/iphone/" title="iphone" rel="tag nofollow">iphone</a>, <a href="http://iphone.longearth.net/tag/navigation-controller/" title="Navigation Controller" rel="tag nofollow">Navigation Controller</a>, <a href="http://iphone.longearth.net/tag/objecti/" title="objecti" rel="tag nofollow">objecti</a>, <a href="http://iphone.longearth.net/tag/objective-c/" title="objective-c" rel="tag nofollow">objective-c</a>, <a href="http://iphone.longearth.net/tag/uitableview/" title="UITableView" rel="tag nofollow">UITableView</a>, <a href="http://iphone.longearth.net/tag/uiviewcontroller/" title="UIViewController" rel="tag nofollow">UIViewController</a>, <a href="http://iphone.longearth.net/tag/xcode/" title="Xcode" rel="tag nofollow">Xcode</a>, <a href="http://iphone.longearth.net/tag/%e3%82%a2%e3%82%a4%e3%82%b3%e3%83%b3/" title="アイコン" rel="tag nofollow">アイコン</a>, <a href="http://iphone.longearth.net/tag/%e3%82%a2%e3%83%97%e3%83%aa/" title="アプリ" rel="tag nofollow">アプリ</a>, <a href="http://iphone.longearth.net/tag/%e3%83%81%e3%83%a5%e3%83%bc%e3%83%88%e3%83%aa%e3%82%a2%e3%83%ab/" title="チュートリアル" rel="tag nofollow">チュートリアル</a>, <a href="http://iphone.longearth.net/tag/%e3%83%91%e3%83%81%e3%82%b9%e3%83%ad/" title="パチスロ" rel="tag nofollow">パチスロ</a>, <a href="http://iphone.longearth.net/tag/%e3%83%aa%e3%82%b9%e3%83%88/" title="リスト" rel="tag nofollow">リスト</a>, <a href="http://iphone.longearth.net/tag/%e3%83%aa%e3%83%aa%e3%83%bc%e3%82%b9/" title="リリース" rel="tag nofollow">リリース</a>, <a href="http://iphone.longearth.net/tag/%e5%8b%95%e7%94%bb/" title="動画" rel="tag nofollow">動画</a>, <a href="http://iphone.longearth.net/tag/%e5%8f%8e%e6%94%af/" title="収支" rel="tag nofollow">収支</a>, <a href="http://iphone.longearth.net/tag/%e7%94%bb%e9%9d%a2%e9%81%b7%e7%a7%bb/" title="画面遷移" rel="tag nofollow">画面遷移</a></p>

	<h4>関連する投稿</h4>
	<ul class="st-related-posts">
	<li><a href="http://iphone.longearth.net/2009/01/20/%e3%80%90uitableview%e3%80%91uitableview%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e8%a9%b3%e7%b4%b0%e7%94%bb%e9%9d%a2%e3%82%92%e4%bd%9c%e3%82%8b-1%e3%80%90uitableviewdatasource%e3%80%91/" title="【UITableView】UITableViewを使って詳細画面を作る-1【UITableViewDataSource】 (2009 年 1 月 20 日)">【UITableView】UITableViewを使って詳細画面を作る-1【UITableViewDataSource】</a> </li>
	<li><a href="http://iphone.longearth.net/2008/12/31/%e3%80%90uitableview%e3%80%91uitableview%e3%81%ab%e6%96%87%e5%ad%97%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/" title="【UITableView】UITableViewに文字を表示する (2008 年 12 月 31 日)">【UITableView】UITableViewに文字を表示する</a> </li>
	<li><a href="http://iphone.longearth.net/2008/12/31/%e3%80%90uitableview%e3%80%91uitableview%e3%81%ab%e3%83%aa%e3%82%b9%e3%83%88%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/" title="【UITableView】UITableViewにリストを表示する (2008 年 12 月 31 日)">【UITableView】UITableViewにリストを表示する</a> </li>
	<li><a href="http://iphone.longearth.net/2009/04/07/%e3%80%90uiviewcontroller%e3%80%91iphone%e3%82%92%e6%a8%aa%e5%90%91%e3%81%8d%e3%81%ab%e3%81%97%e3%81%9f%e6%99%82%e3%81%ab%e5%88%a5%e7%94%bb%e9%9d%a2%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b/" title="【UIVIewController】iPhoneを横向きにした時に別画面を表示する方法 (2009 年 4 月 7 日)">【UIVIewController】iPhoneを横向きにした時に別画面を表示する方法</a> </li>
	<li><a href="http://iphone.longearth.net/2009/02/26/%e3%80%90uitableview%e3%80%91grouped%e3%81%aa%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%ae%e3%82%b9%e3%82%af%e3%83%ad%e3%83%bc%e3%83%ab%e3%81%8c%e9%87%8d%e3%81%84/" title="【UITableView】Groupedなテーブルのスクロールが重い (2009 年 2 月 26 日)">【UITableView】Groupedなテーブルのスクロールが重い</a> </li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://iphone.longearth.net/2009/01/19/%e3%80%90uitableview%e3%80%91%e9%81%b8%e6%8a%9e%e8%a1%8c%e3%81%8b%e3%82%89%e5%88%a5%e7%94%bb%e9%9d%a2%e3%82%92%e5%91%bc%e3%81%b3%e5%87%ba%e3%81%99/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
