xcode 通用 iPhone/iPad AppDelegate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3844296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Universal iPhone/iPad AppDelegate
提问by Doz
I was wondering if anyone would be able to tell me, starting a new app from scratch, how would I organise having a universal (iPhone/iPad) iOS app. I noticed using the default template with the xCode Beta, it gives you both a shared AppDelegate and subclassed appdelegates for iPhone and iPad.
我想知道是否有人能够告诉我,从头开始一个新的应用程序,我将如何组织一个通用的(iPhone/iPad)iOS 应用程序。我注意到在 xCode Beta 中使用默认模板,它为您提供了共享的 AppDelegate 和适用于 iPhone 和 iPad 的子类 appdelegates。
Now, how do you put in the logic to determine which app delegate to use, how does it know which one to instantiate and work with as the default template doesnt state. If I were to write in the iPhone appDelegate, how do i know that this will only run for the iPhone iOS for example?
现在,您如何放入逻辑以确定要使用哪个应用程序委托,它如何知道要实例化和使用哪个应用程序委托,因为默认模板没有说明。如果我要在 iPhone appDelegate 中编写,我怎么知道这只会在 iPhone iOS 上运行?
采纳答案by Sagar
Use following code
使用以下代码
id<UIApplicationDelegate> delegate = [[UIApplication sharedApplication] delegate];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
AppDelegate_iPad *appDelegate = (AppDelegate_iPad *) delegate;
else
AppDelegate_iPhone *appDelegate = (AppDelegate_iPhone *) delegate;
whenever you are required to access to the delegate.
每当您需要访问委托时。
回答by hotpaw2
The Info.plist ("Main nib file base name") for an app specifies a main window .xib file to load, and that .xib file specifies an app delegate and root window controller for the app.
应用程序的 Info.plist(“主 nib 文件基本名称”)指定要加载的主窗口 .xib 文件,该 .xib 文件指定应用程序的应用程序委托和根窗口控制器。
The Info.plist for an iPad ("NSMainNibFile~ipad") additionally specifies a main window .xib file for the iPad, which can contain a different app delegate and a different root window controller for the app. Or they could be the same ones, using runtime UIUserInterfaceIdiomPad checks.
iPad 的 Info.plist(“NSMainNibFile~ipad”)另外为 iPad 指定了一个主窗口 .xib 文件,它可以包含不同的应用程序委托和不同的应用程序根窗口控制器。或者它们可以是相同的,使用运行时 UIUserInterfaceIdiomPad 检查。
The runtime, which knows what device type on which it's running, picks the correct initial .xib file to load from the info.plist, and from there you get the correct app delegate and root window controller configured and running.
运行时,它知道它正在运行的设备类型,选择正确的初始 .xib 文件从 info.plist 加载,从那里你得到正确的应用程序委托和根窗口控制器配置和运行。
回答by johndpope
Variations on a theme
一个主题的变化
Create a Common.h file
创建一个 Common.h 文件
// Global Helpers
#define APP_DELEGATE() (AppDelegate *)[[UIApplication sharedApplication] delegate]
#define APP_DELEGATE_IPAD() (AppDelegate_iPad*) [[UIApplication sharedApplication] delegate];
#define APP_DELEGATE_IPHONE() (AppDelegate_iPhone*) [[UIApplication sharedApplication] delegate];
#define IS_IPAD ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] && [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
Import this Common.h / AppDelegate_iPhone.h / AppDelegate_iPad.h /AppDelegate.h in your precompiled header .pch file
将此 Common.h / AppDelegate_iPhone.h / AppDelegate_iPad.h /AppDelegate.h 导入到您的预编译头 .pch 文件中
then in your class you can simply call
然后在你的课堂上你可以简单地打电话
if (IS_IPAD) {
AppDelegate_iPad *appDelegate = APP_DELEGATE_IPAD();
}else{
AppDelegate_iPhone *appDelegate = APP_DELEGATE_IPHONE();
}
回答by MechEngineer
Continuing on Sagar's answer, here is code that will fix Ryan Waggoner's comment.
继续 Sagar 的回答,这里是修复 Ryan Waggoner 评论的代码。
id<UIApplicationDelegate> delegate = [[UIApplication sharedApplication] delegate];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
delegate = (AppDelegate_iPad *) delegate;
else
delegate = (AppDelegate_iPhone *) delegate;
The problem with the previous answer is that it doesn't actually give access to the variable delegate
outside the IF statement. This code simply casts the original variable to the proper type.
上一个答案的问题在于它实际上并没有提供delegate
对 IF 语句之外的变量的访问权限。此代码只是将原始变量转换为正确的类型。
回答by Wim Haanstra
I am doing the same thing at the moment and I learned a lot from the following tutorial:
我目前正在做同样的事情,我从以下教程中学到了很多东西:
http://iphonedevelopment.blogspot.com/2010/04/converting-iphone-apps-to-universal.html
http://iphonedevelopment.blogspot.com/2010/04/converting-iphone-apps-to-universal.html
This has helped me out to setup the project to support iPad and iPhone devices.
这帮助我设置项目以支持 iPad 和 iPhone 设备。