'keyWindow' 已弃用:首先在 iOS 13.0 中弃用 Objective-C

'keyWindow' is deprecated: first deprecated in iOS 13.0 in Objective-C

这是我显示 XIB 文件的简单按钮操作,它实际上是 Objective-C 中的条码扫描器。

 - (IBAction)startCamera:(id)sender {
    BarcodeVC * controller = [[BarcodeVC alloc] initWithNibName:@"BarcodeVC" bundle:[NSBundle mainBundle]];
    
    //[self presentViewController:controller animated:YES completion:nil];
     UIWindow * currentwindow = [[UIApplication sharedApplication] keyWindow];
     [currentwindow.rootViewController presentViewController:controller animated:YES completion:nil];
  }

但不幸的是,出现了一个警告:

keyWindow is deprecated: first deprecated in iOS 13.0

我知道因为iOS13支持多场景,但是Objective-C有没有办法解决这个问题?我看过 Swift 个版本,但 Objective-C.

没有成功

您可以通过 AppDelegate class 使用 window,例如..

BarcodeScannerVC * controller = [[BarcodeScannerVC alloc] initWithNibName:@"BarcodeScannerVC" bundle:[NSBundle mainBundle]];

//[self presentViewController:controller animated:YES completion:nil];
UIWindow * currentwindow = [[UIApplication sharedApplication] delegate].window;
[currentwindow.rootViewController presentViewController:controller animated:YES completion:nil];

这里我更改为获取当前window行,所以只需更改即可。

UIWindow * currentwindow = [[UIApplication sharedApplication] delegate].window;