【发布时间】:2014-08-18 07:18:06
【问题描述】:
我有一个希望在外部屏幕上显示的应用程序。
问题是,当我进入硬件 -> 外部显示器并选择其中之一时 - 不会触发事件。为什么?
这也不会被输入:
if ([[UIScreen screens] count] > 1)
所以我添加了下一个代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//SOME CODE ...
[self checkForExistingScreenAndInitializeIfPresent];
[self setUpScreenConnectionNotificationHandlers];
return YES:
}
- (void)checkForExistingScreenAndInitializeIfPresent
{
if ([[UIScreen screens] count] > 1)
{
// Get the screen object that represents the external display.
UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];
// Get the screen's bounds so that you can create a window of the correct size.
CGRect screenBounds = secondScreen.bounds;
self.secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
self.secondWindow.screen = secondScreen;
self.externalWindow=[[ExternalDisplayViewController alloc]initWithNibName:@"ExternalDisplayViewController" bundle:nil];
self.externalWindow.view.frame=screenBounds;
self.secondWindow.rootViewController=self.externalWindow;
// Set up initial content to display...
// Show the window.
self.secondWindow.hidden = NO;
}
}
- (void)setUpScreenConnectionNotificationHandlers
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(handleScreenDidConnectNotification:)
name:UIScreenDidConnectNotification object:nil];
[center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:)
name:UIScreenDidDisconnectNotification object:nil];
}
补充:
刚刚尝试在 ViewDidLoad 中添加代码
添加了这个:
// Check for external screen.
if ([[UIScreen screens] count] > 1)
{
}
else {
}
已打开外接显示器和模拟器 - 不进入 IF 块
【问题讨论】:
-
我认为您应该在视图控制器中拥有此代码,而不是在应用程序委托中。我有和你类似的代码,而且效果很好。
-
从这里下载的源代码:mattgemmell.com/ipad-vga-output 也不起作用。也许我必须在模拟器中启用某些东西?
-
我几乎可以肯定您必须在
UIViewController子类中包含此代码。视图控制器“拥有”所有视图,内部和外部。 -
我刚刚检查了我的旧代码,它与您所拥有的非常相似(只是包含在视图控制器中),并且按预期工作。
-
这行代码
[self checkForExistingScreenAndInitializeIfPresent];应该出现在viewDidLoad:和通知处理程序中。
标签: ios ipad ios-simulator external-display