Ios 如果应用程序委派要使用主情节提要文件,则必须实现window属性
我已经尝试了在stackoverflow中找到的所有解决方案,但都不起作用 这是我当前的AppDelegate.swift文件,我想我正在以某种方式实现窗口属性:Ios 如果应用程序委派要使用主情节提要文件,则必须实现window属性,ios,xcode,objective-c,swift,Ios,Xcode,Objective C,Swift,我已经尝试了在stackoverflow中找到的所有解决方案,但都不起作用 这是我当前的AppDelegate.swift文件,我想我正在以某种方式实现窗口属性: import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinis
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow()
window?.makeKeyAndVisible()
let mainVC = UIViewController()
window?.rootViewController = mainVC
return true
}
// MARK: UISceneSession Lifecycle
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
window = UIWindow()
window?.makeKeyAndVisible()
let mainVC = UIViewController()
window?.rootViewController = mainVC
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
window = UIWindow()
window?.makeKeyAndVisible()
let mainVC = UIViewController()
window?.rootViewController = mainVC
}
导入UIKit
@UIApplicationMain
类AppDelegate:UIResponder、UIApplicationLegate{
变量窗口:UIWindow?
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.launchOptions键:任意]?)->Bool{
//应用程序启动后自定义的覆盖点。
window=ui窗口()
窗口?.makeKeyAndVisible()
让mainVC=UIViewController()
窗口?.rootViewController=mainVC
返回真值
}
//MARK:UISceneSession生命周期
@可用(iOS 13.0,*)
func应用程序(uApplication:UIApplication,用于连接的配置SceneSession:UISceneSession,选项:UIScene.ConnectionOptions)->UISceneConfiguration{
//在创建新场景会话时调用。
//使用此方法可以选择配置以创建新场景。
window=UIWindow()
窗口?.makeKeyAndVisible()
让mainVC=UIViewController()
窗口?.rootViewController=mainVC
返回UISceneConfiguration(名称:“默认配置”,会话角色:ConnectionSceneSession.role)
}
@可用(iOS 13.0,*)
func应用程序(application:UIApplication,didDiscardSceneSessions sceneSessions:Set){
//当用户放弃场景会话时调用。
//如果在应用程序未运行时丢弃了任何会话,则将在application:didFinishLaunchingWithOptions后不久调用该会话。
//使用此方法释放特定于被丢弃场景的任何资源,因为它们不会返回。
window=UIWindow()
窗口?.makeKeyAndVisible()
让mainVC=UIViewController()
窗口?.rootViewController=mainVC
}
}
会发生什么情况,使警告仍然出现,而我的应用程序继续显示黑色屏幕?我会一步一步地指导您
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
如果您正在开发Objective C应用程序,并且遇到此错误,请按照上面提到的相同步骤进行操作,只需在步骤1中进行更改。
添加@property(非原子,保留)窗口;要AppDelegate.h在Xcode 11.4及更高版本中创建新项目,请使用SceneDelegate文件创建项目目录结构 如果你试图运行一个项目,你会得到一个错误 步骤1:删除SceneDelegate.h和SceneDelegate.m 步骤2:在AppDelegate.h中创建窗口属性
@property (strong, nonatomic) UIWindow *window;
步骤3:在主情节提要中,为ViewController设置情节提要id
并在appdelegate的didFinishLaunchingWithOptions方法中添加一个设置窗口
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"ViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
对于Swift:使用以下代码
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey:
Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
}
注意:不要在didFinishLaunchingWithOptions方法中为swift添加窗口。
我刚刚在AppDelegate类中添加了
var window:UIWindow?
,问题得到了解决。完成了所有指示,但不起作用,如果我没有弄错的话,我已经尝试过了,无论如何感谢您的帮助。我认为您没有在故事板中设置入口点。如果未设置,则更新func,如下面的代码所示。func应用程序(application:UIApplication,didFinishLaunchWithOptions launchOptions:[UIApplication.launchOptions Key:Any]?)->Bool{let navigation=UINavigationController(rootViewController:ViewController())let frame=UIScreen.main.bounds window=UIWindow(frame:frame)window!.rootViewController=navigation window!.makeKeyAndVisible()return true}仍然一样,这真是一个让人头疼的问题,无论如何,谢谢你的帮助。我也为swift添加了答案。请检查以上,谢谢,但我使用的是Swift。答案非常适合Swift。与目标C相同的步骤,添加@property(非原子,保留)IBOutlet UIWindow;到AppDelegate.h
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey:
Any]?) -> Bool {
// Override point for customization after application launch.
return true
}