【问题标题】:iPad simulator and external screeniPad模拟器和外屏
【发布时间】: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


【解决方案1】:

这让我发疯了。 我使用的是 10.0 (10A255) 版,但它无法正常工作。我正在查看 application:didFinishLaunchingWithOptions: for a 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.
        }
    }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      这是我在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
          }
      } 
      

      它应该可以进行细微的更改。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 你没听懂 - 问题是它没有进入 IF 块 - 没有检测到模拟的外部显示器
        • 检查已编辑的。您需要处理连接并需要调用checkForExistingScreenAndInitializeIfPresent
        • 我有编写的处理程序,只是没有将它们粘贴到这里。问题是,如果我禁用或启用外部显示器,该代码不会输入它们
        • 从这里下载的源代码:mattgemmell.com/ipad-vga-output 也不起作用。也许我必须在模拟器中启用某些东西?
        • 与模拟器无关,除了启用 .此外,您需要对代码做的所有事情。
        猜你喜欢
        • 2013-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-03
        • 2012-12-16
        • 1970-01-01
        • 2021-01-02
        相关资源
        最近更新 更多