我有一个应用程序,希望在外部屏幕上显示。

问题是,当我转到硬件 - > 外部显示器并选择其中之一时 - 不会触发事件。为什么?

这也不会被输入:

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 块

最佳答案

问题可能与 ExternalDisplayViewController 的初始化有关:

self.externalWindow=[[ExternalDisplayViewController alloc]initWithNibName:@"ExternalDisplayViewController" bundle:nil];

试试这个 :
[[ExternalDisplayViewController alloc]initWithNibName:@"ExternalDisplayViewController" bundle:nil];
 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.externalWindow= = [storyboard instantiateViewControllerWithIdentifier:@"ExternalDisplayView"];

而且,您需要覆盖此 handleScreenDidConnectNotification
-(void)handleScreenDidConnectNotification : (NSNotification *)aNotification{
    UIScreen *newScreen = [aNotification object];
    CGRect screenBounds = newScreen.bounds;
    self.alertForNotifyDisplay =  [[UIAlertView alloc] initWithTitle:@"External Display Connected." message:Nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [self.alertForNotifyDisplay show];

    if (!self.extWindow) {
        self.extWindow  = [[UIWindow alloc] initWithFrame:screenBounds];
        self.extWindow.screen = newScreen;

        [self checkForExistingScreenAndInitializeIfPresent];
    }

}

关于ios - iPad模拟器和外屏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24447427/

10-10 14:42