4

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

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

这也不会被输入:

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 块

4

4 回答 4

0

这是我的UIViewController子类中的完整代码。我已经用 7.1 模拟器对其进行了检查,它对我有用(我启动应用程序,然后在它已经运行后初始化外部显示器):

- (void)viewDidLoad {
    // Other viewDidLoad code…
    // Check and initialize big screen
    [self checkForExistingScreenAndInitializeIfPresent]; 
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // Register for second screen notifications
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(handleScreenDidConnectNotification:)
               name:UIScreenDidConnectNotification object:nil];
    [center addObserver:self selector:@selector(handleScreenDidDisconnectNotification:)
               name:UIScreenDidDisconnectNotification object:nil];
}

- (void)handleScreenDidConnectNotification:(NSNotification *)notification {
    [self checkForExistingScreenAndInitializeIfPresent];
}

- (void)handleScreenDidDisconnectNotification:(NSNotification *)notification {
    [self checkForExistingScreenAndInitializeIfPresent];
}

- (void)checkForExistingScreenAndInitializeIfPresent {
    if ([[UIScreen screens] count] > 1) {
        // Get the screen object that represents the external display.
        UIScreen *secondScreen = [[UIScreen screens] objectAtIndex:1];
        secondScreen.currentMode = secondScreen.preferredMode;
        secondScreen.overscanCompensation = 3;
        // Get the screen's bounds so that you can create a window of the correct size.
        CGRect screenBounds = CGRectMake(secondScreen.bounds.origin.x,
                                         secondScreen.bounds.origin.y,
                                         secondScreen.currentMode.size.width,
                                         secondScreen.currentMode.size.height);

        UIWindow *secondWindow = [[UIWindow alloc] initWithFrame:screenBounds];
        secondWindow.screen = secondScreen;
        // Setup external VC
        [ExternalScreenViewController sharedExternalScreen].window = secondWindow;
        // Set VC for second window
        secondWindow.rootViewController = [ExternalScreenViewController sharedExternalScreen];
        // Show the window.
        secondWindow.hidden = NO;
    } else {
        // What to do if disconnected
    }
} 

它应该与微小的变化一起工作。

于 2014-06-27T10:29:27.620 回答
0

问题出在 OS 和 Xcode 的 beta 版本中。降级回小牛队 - 一切都像魅力一样

于 2014-07-15T05:46:04.643 回答
0

问题可能与 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];
    }

}
于 2014-06-27T08:46:47.467 回答
0

这让我发疯了。我使用的是 10.0 (10A255) 版,但它无法正常工作。我正在查看 application:didFinishLaunchingWithOptions: 的原因是 UIScreen.screens.count > 1

这将永远是 1

而是试试这个

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   let center = NotificationCenter.default
   center.addObserver(self, selector: #selector(didConnect(notification:)), name: UIScreen.didConnectNotification, object: nil)
   return true
}

@objc func didConnect(notification: Notification) {
    if UIScreen.screens.count > 1 {
        if let screen = UIScreen.screens.last {
            let window = UIWindow(frame: screen.bounds)
            window.screen = screen

            let vc = UIViewController(nibName: nil, bundle: nil)
            vc.view.backgroundColor = .red

            window.isHidden = false
            window.rootViewController = vc
            self.secondWindow = window // Will not show unless window variable is retained.
        }
    }
}
于 2018-11-23T18:44:11.957 回答