我有一个应用程序,我希望在外部屏幕上显示。
问题是,当我去硬件 - >外部显示并选择其中一个 - 事件未被触发。为什么呢?
这也没有输入:
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块
答案 0 :(得分: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];
}
}
答案 1 :(得分: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
}
}
它应该可以进行微小的改动。
答案 2 :(得分:0)
问题出在OS和Xcode的beta版本中。降级回到小牛队 - 所有的作品都像魅力一样
答案 3 :(得分:0)
这真让我发疯。 我正在使用版本10.0(10A255),但无法正常工作。我之所以要查看application:didFinishLaunchingWithOptions:for 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.
}
}
}