私はアプリケーションを開発しています。シングル ビュー ベースのアプリケーション モデルを使用してユニバーサル アプリケーションを作成しています。そのため、新しいクラスを作成する必要があります。ただし、xib は 1 つだけです。iPhone と iPad 用に 2 つの xib が必要です。1 つのクラスに対して 2 つの xib を作成する方法を教えてください。
2 に答える
同じ名前で新しいものを作成します..あなたのView Controller名が「NewViewController」であるとしましょう..あなたのxibはNewViewController~ipad
iPad用とNewViewController~iPhone
iphone用になります..実装するときは、initWithNibName
あなたのxibの基本的な名前を書くだけです. NewViewController
iOS は、現在使用されているプラットフォームに基づいて一致する xib の呼び出しを処理します。下の画像のように、新しい xib のファイル所有者のカスタム クラスを新しいクラスに割り当てることを忘れないでください。
新しいxibを作成するには、これらの画像を確認してください:
Malek_Jundi には、iPhone および iPad 用の .nib ファイルを作成およびロードする方法に関する明確なガイドがあります。
ケース (iphone または ipad) ごとに異なるクラスを作成する場合は、次のように IF ステートメントを使用できます。
UIViewController *target;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
target = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:[NSBundle mainBundle]];
} else {
target = [[NewViewController_ipad alloc] initWithNibName:@"NewViewController" bundle:[NSBundle mainBundle]];
}
しかし、コードに「IF」ステートメントを繰り返し入力して、iphone/ipad 用の特定のクラスを作成するのが面倒です。別の方法があります:
- (Class)idiomClassWithName:(NSString*)className
{
Class ret;
NSString *specificName = nil;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
specificName = [[NSString alloc] initWithFormat:@"%@_ipad", className];
} else {
specificName = [[NSString alloc] initWithFormat:@"%@_iphone", className];
}
ret = NSClassFromString(specificName);
if (!ret) {
ret = NSClassFromString(className);
}
return ret;
}
- (void)createSpecificNewController
{
Class class = [self idiomClassWithName:@"NewViewController"];
UIViewController *target = [[class alloc] initWithNibName:@"NewViewController" bundle:[NSBundle mainBundle]];
//...
}