I have a universal iOS app for iPad and iPhone. I created a class with a xib and its for the iPad. How do I design it for the iPhone now? Create another xib for iPhone? If so, how and where do I tell the app to load the xib for the iPhone when needed, because right now it loads the iPad xib and everything is too big of course. Thanks in advance
3 に答える
そのようなアプリケーションを実行する必要があるときは、iPad用とiPhone用の2つのxibファイルを作成しました。1つ目は、ViewControllerを使用して作成されます。2つ目は、インターフェイスファイルを追加するだけで作成されます。意味のある名前を取得しようとしています(例:ListNameVC.xibおよびListNameVC_iPad.xib
2番目のxibでは、2つのことを行う必要があります
- ファイルの所有者クラスは正しいVCクラスを指している必要があります
- オブジェクト内のビューをファイルの所有者内のビューアウトレットにリンクする必要があります(そうしないと、実行時にクラッシュします)。
以上で、View Controllerの作成時に、デバイスに基づいて正しいxibを呼び出すことができます。
ListeNameVC* listeNameVC
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
listeNameVC = [[ListeNameVC alloc] initWithNibName:@"ListeNameVC_iPad" bundle:nil];
} else {
listeNameVC = [[ListeNameVC alloc] initWithNibName:@"ListeNameVC" bundle:nil];
}
iPhoneまたはiPadで別のことを行う必要がある場合は、ViewControllerでUI_USER_INTERFACE_IDIOMを使用できます。たとえば、UIButtonをプログラムで配置した場合、またはiPhone用とiPad用に別の名前で画像を設定した場合
Cdt
さて、あなたのビューコントローラには。と呼ばれるメソッドがありますinitWithNibName: bundle:
。これを行う:
MyController *controller = [[MyController alloc] initWithNibName:[NSString stringWithFormat:@"myController_%@",deviceName] bundle:nil];
編集:あなたは2つのxibsを作らなければなりませんよね?Settings_iPhone.xibやSettings_iPad.xibのような名前を付けます。設定画面を表示したい場合は、先に進んで宣言してください
NSString *deviceName = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) ? @"iPhone" : @"iPad";
その後 :
Settings *controller = [[Settings alloc] initWithNibName:[NSString stringWithFormat:@"Settings_%@",deviceName] bundle:nil];
はい、2つのnibファイルが必要です。initWithNibName
次のコードを使用してデバイスタイプを検出することにより、ペン先の名前を使用および指定できます。
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
これにより、異なるデバイスを区別できるようになります