在AppDelegate中更改根视图控制器时出现黑屏

时间:2017-04-18 21:20:58

标签: ios swift3 xcode8 swrevealviewcontroller

我正在尝试从应用委托的ActiveCell.FormulaR1C1 = "=""["" & """ & field & """ & ""]="" & RC[-2] & RC[-1]" 更改根视图控制器,具体取决于用户是否已登录。一旦我超过这个条件,我使用以下代码来更改根视图控制器:

didFinishLaunchingWithOptions

然而,当我启动应用程序(使用有效的登录用户)时,模拟器首先显示登录屏幕(旧根视图控制器)一秒钟,然后屏幕变黑约30秒到一分钟,之后最后显示所需的视图控制器。

故事板中的视图控制器结构如下:

SWRevealViewController - >导航控制器 - >期望的视图控制器(新根)

self.window = UIWindow(frame: UIScreen.main.bounds) self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController self.window?.makeKeyAndVisible() 开头的原因是因为幻灯片菜单丢失了。

有关可能发生的事情的任何想法?

4 个答案:

答案 0 :(得分:0)

以下是一个适合我的示例,只是让您的loginViewControllers成为UINavigationController的堆栈,与SWRevealController无关。这很容易解决。

self.window = UIWindow(frame: UIScreen.main.bounds)
if User.shared.firstLaunch {
     let navigationVC = Storyboard.inital.instantiateViewController(withIdentifier: "firstLaunchNC") as! UINavigationController
     self.window?.rootViewController = navigationVC
} else if User.shared.token == "" {
     let navigationVC = Storyboard.inital.instantiateViewController(withIdentifier: "initialVC") as! UINavigationController
     self.window?.rootViewController = navigationVC
} else {
     self.registerForPushNotifications(application: UIApplication.shared)
     User.shared.refreshToken()
     let revealController = Storyboard.main.instantiateViewController(withIdentifier: "revealViewController") as! SWRevealViewController
     self.window?.rootViewController = revealController
}

self.window?.makeKeyAndVisible()
return true

答案 1 :(得分:0)

我找到了一种方法来产生与我想要的结果相似的结果。它涉及根本不更改根视图控制器,并在启动后呈现“人工根视图控制器”:

if let currentRoot = self.window?.rootViewController {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let artificialRoot = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
    currentRoot.present(artificialRoot, animated: false, completion: nil)
}

虽然不理想,但这种实现的结果远远好于问题描述中的实现。

答案 2 :(得分:0)

试试这个:

let viewController = ViewController()
let navigationController = UINavigationController(rootViewController: viewController)
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()

答案 3 :(得分:0)

尝试一下:

 UIStoryboard *storyboard  = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            UITabBarController *tbc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarController"];
            tbc.selectedIndex=[singleton getSelectedTabIndex];
            // tbc.selectedIndex=0;
            //self.window.rootViewController = tbc;
            [self.window setRootViewController:tbc];

            [UIView transitionWithView:self.window duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil
     

completion:nil];

如果这不起作用..请检查您的初始Controller ...不要尝试加载NSAtrributedString

  

NSAttributedString * attrStr = [[NSAttributedString alloc]   initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding]   选项:@ {NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,   NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}                                                               documentAttributes:无                                                                            错误:无];

注意:-初始控制器ViewDidLoad出现黑屏时,由于一些惰性UI加载,估计需要在ViewDidLoad中完成,因此需要一些时间将UIview加载到内存中。

尝试将重载视图放入ViewWillApear或ViewDidApear...。