Using UIScreen to drive a VGA display - doesn't seem to show the UIWindow?

12,198

Solution 1

You need to init your window...

 UIWindow *newwindow = [[UIWindow alloc] init];

Solution 2

[newwindow makeKeyAndVisible];?

Solution 3

I think your problem is the externalDisplay. Create a viewcontroller outside your code (maybe do a simply Add new file ViewController and put stuff in the .xib) and try it out to make sure that viewcontroller is working before you call it into the external display. Here's your code with my suggested changes -- [mainViewController view] is the viewcontroller you created outside.

//Code to detect if an external display is connected to the iPad.
NSLog(@"Number of screens: %d", [[UIScreen screens]count]);

//Now, if there's an external screen, we need to find its modes, iterate
//through them and find the highest one. Once we have that mode, break out,
//and set the UIWindow.

if([[UIScreen screens]count] > 1) //if there are more than 1 screens connected
                                  //to the device
{
 CGSize max;
 UIScreenMode *maxScreenMode;
 for(int i = 0; i < [[[[UIScreen screens] objectAtIndex:1] availableModes]count]; i++)
 {
  UIScreenMode *current = [[[[UIScreen screens]objectAtIndex:1]availableModes]objectAtIndex:i];
  if(current.size.width > max.width);
  {
   max = current.size;
   maxScreenMode = current;
  }
 }
 //Now we have the highest mode. Turn the external display to use that mode.
 UIScreen *external = [[UIScreen screens] objectAtIndex:1];
 external.currentMode = maxScreenMode;
 //Boom! Now the external display is set to the proper mode. We need to now
 //set the screen of a new UIWindow to the external screen

 UIWindow *newwindow = [UIWindow alloc];

 [newwindow addSubview:[mainViewController view]];
 newwindow.screen = external;

 [newwindow makeKeyAndVisible];
 [newwindow setHidden:NO];
}

Solution 4

Just recording this here in case anyone stumbles on this question. I wasn't able to get anything to show up on the second screen until I realized that my app delegate had to retain the UIWindow. It has no natural owner, so if you just do a regular autorelease, the window will be released before it ever displays.

Hope that helps.

Solution 5

I've uploaded sample .xcodeproj into github.

I refered to this page mainly.

Thanks a lot. :)

http://github.com/igaiga/iPadDisplayOutSample

Share:
12,198
Peter Hajas
Author by

Peter Hajas

Hi there! I'm a developer, really interested in all things computers, especially User Experience and User Interface Design!

Updated on June 13, 2022

Comments

  • Peter Hajas
    Peter Hajas almost 2 years

    HI there,

    I'm trying to use UIScreen to drive a separate screen with the VGA dongle on my iPad.

    Here's what I've got in my root view controller's viewDidLoad:

    //Code to detect if an external display is connected to the iPad.
     NSLog(@"Number of screens: %d", [[UIScreen screens]count]);
    
     //Now, if there's an external screen, we need to find its modes, itereate through them and find the highest one. Once we have that mode, break out, and set the UIWindow.
    
     if([[UIScreen screens]count] > 1) //if there are more than 1 screens connected to the device
     {
      CGSize max;
      UIScreenMode *maxScreenMode;
      for(int i = 0; i < [[[[UIScreen screens] objectAtIndex:1] availableModes]count]; i++)
      {
       UIScreenMode *current = [[[[UIScreen screens]objectAtIndex:1]availableModes]objectAtIndex:i];
       if(current.size.width > max.width);
       {
        max = current.size;
        maxScreenMode = current;
       }
      }
      //Now we have the highest mode. Turn the external display to use that mode.
      UIScreen *external = [[UIScreen screens] objectAtIndex:1];
      external.currentMode = maxScreenMode;
      //Boom! Now the external display is set to the proper mode. We need to now set the screen of a new UIWindow to the external screen
      external_disp = [externalDisplay alloc];
      external_disp.drawImage = drawViewController.drawImage;
      UIWindow *newwindow = [UIWindow alloc];
      [newwindow addSubview:external_disp.view];
      newwindow.screen = external;
     }