1

私はアプリケーションを開発しています。シングル ビュー ベースのアプリケーション モデルを使用してユニバーサル アプリケーションを作成しています。そのため、新しいクラスを作成する必要があります。ただし、xib は 1 つだけです。iPhone と iPad 用に 2 つの xib が必要です。1 つのクラスに対して 2 つの xib を作成する方法を教えてください。

4

2 に答える 2

3

同じ名前で新しいものを作成します..あなたのView Controller名が「NewViewController」であるとしましょう..あなたのxibはNewViewController~ipadiPad用とNewViewController~iPhoneiphone用になります..実装するときは、initWithNibNameあなたのxibの基本的な名前を書くだけです. NewViewControlleriOS は、現在使用されているプラ​​ットフォームに基づいて一致する xib の呼び出しを処理します。下の画像のように、新しい xib のファイル所有者のカスタム クラスを新しいクラスに割り当てることを忘れないでください。

ここに画像の説明を入力

新しいxibを作成するには、これらの画像を確認してください:

ここに画像の説明を入力

ここに画像の説明を入力

于 2012-06-20T10:08:46.430 に答える
0

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]];
    //...
}
于 2014-02-12T03:23:40.250 に答える