选择 iPad 和 iPhone 的启动视图

发布于 2024-12-21 01:41:30 字数 456 浏览 3 评论 0原文

我的第一个应用程序仅适用于 iPhone,我决定也实现在 iPad 上使用的功能,并且我想要选择的第一件事是在打开 iPhone 或 iPad 时选择要使用的视图,在我的代表中写道:

NSString *devy = [[UIDevice currentDevice]model];
if ([devy isEqualToString:@"iPad"]) {
    [window addSubview:viewController.viewCpuIpad];
    [window makeKeyAndVisible]; 
} else {
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];    
}

当 iPhone 时没问题,但当我在 iPad 上打开它时仅显示空白屏幕....哪里出了问题?

谢谢

my first application that was just for the iPhone I decided to also implement the ability to be used on the iPad and the first thing I wanted to choose the view to use if it were in the opening of an iPhone or iPad, in my delegate I wrote :

NSString *devy = [[UIDevice currentDevice]model];
if ([devy isEqualToString:@"iPad"]) {
    [window addSubview:viewController.viewCpuIpad];
    [window makeKeyAndVisible]; 
} else {
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];    
}

When the iPhone's okay, but when I open it on the iPad only displays a blank screen .... where wrong?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话已封尘 2024-12-28 01:41:30

Apple推荐的方式(取自Xcode的通用应用程序项目模板)是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

如果您不想使用IB,您可以在视图控制器的loadView方法中创建视图,例如:

- (void)loadView
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.view = // create view for iPad.
    } else {
        self.view = // create view for iPhone.
    }
}

(UI_USER_INTERFACE_IDIOM()是支持iOS 3.1及更早版本的宏。 )

Apple's recommended way (taken from Xcode's universal application project template) is:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
    } else {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

If you don't want to use IB, you can create your view in view controller's loadView method like:

- (void)loadView
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.view = // create view for iPad.
    } else {
        self.view = // create view for iPhone.
    }
}

(UI_USER_INTERFACE_IDIOM() is a macro to support iOS 3.1 and earlier.)

尛丟丟 2024-12-28 01:41:30

当您在 iPad 中启动时,不要调用

[window addSubview:viewController.viewCpuIpad];

尝试

viewController.view = viewCpuIPad;

[window addSubview: viewController.view];

我的建议是,当您在 iPad 上启动时,不要更新 viewController.view 属性。

默认情况下,当您创建 nib 时,viewController.view 设置为为 iPhone 显示的 nib 视图。

对于 iPad,您应该将此 [viewcontroller view] 属性更新为您指定的视图。另外,正如 Hoshi 所建议的,拥有两个不同的 Nib 文件也是一个好方法,以便您可以根据条件加载所需的 nib。

希望这有帮助:)

When you are launching in iPad, instead of calling,

[window addSubview:viewController.viewCpuIpad];

try like,

viewController.view = viewCpuIPad;

[window addSubview:viewController.view];

My suggestion is, you are not updating the viewController.view property when you are launching on iPad.

By Default, when you created the nib, the viewController.view is set to nib view which is displayed for iPhone.

For iPad, you are supposed to update this [viewcontroller view] property to your designated view. Else, as suggested by Hoshi, its also a good way to have two different Nib files so that you can load the required nib based on the condition.

Hopes this helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文