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

Fio*_*off -1 objective-c uiwindow ios ios13 uiscenedelegate

这是我的简单按钮操作,用于显示 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];
  }
Run Code Online (Sandbox Code Playgroud)

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

keyWindow已弃用:首先在 iOS 13.0 中弃用

我知道iOS 13支持多场景,但是Objective-C有没有办法解决这个问题?我见过 Swift 版本,但使用 Objective-C 却没有成功。

小智 7

您可以通过 AppDelegate 类使用窗口,例如..

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];
Run Code Online (Sandbox Code Playgroud)

这里我更改为仅获取当前窗口行,因此只需更改它即可。

UIWindow * currentwindow = [[UIApplication sharedApplication] delegate].window;
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

7690 次

最近记录:

4 年,4 月 前