如何在iOS中从appdelegate呈现不同的视图控制器

3

实际上,我将navigationcontroller设置为根控制器,并将其嵌入主.storyboard中。我有两个屏幕:一个登录屏幕和另一个主屏幕。根据登录凭证,我需要跳过登录屏幕并显示主屏幕。从appdelegate中进行此跳过操作时出现了问题。

Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0x7fadf384c600>.

 let storyboard=UIStoryboard.init(name: "Main", bundle: nil)
        let navigationController=storyboard.instantiateInitialViewController()
        let username=UserDefaultUtil.getString(key: AppConstants.PREF_USERID)
        print(username!)
        if username != ""
        {
            window?.rootViewController=navigationController
            let sectionController=SectionController(nibName: "SectionController" , bundle: nil)
            navigationController?.present(sectionController, animated: true, completion: nil)
        }

1
在首页的viewWillAppear方法中执行它。 - Sachin Vas
2个回答

3
我猜您想在导航控制器中显示sectionController,但实际上不是这样工作的。请尝试以下代码:
let navigationController = self.storyboard?.instantiateInitialViewController() as! UINavigationController

并将当前内容替换为以下内容:

navigationController.setViewControllers([sectionController], animated: false)

或者只需删除navigationController实例并使用代码创建它,并将其设置为window?.rootViewController

let sectionController=SectionController(nibName: "SectionController" , bundle: nil)
let nav = UINavigationController(rootViewController: sectionController)
window?.rootViewController = nav

0

首先,在登录页面中检查用户凭据。然后使用:

if hasCredentials {
let vc:AnyObject! = self.storyboard?.instantiateViewController(withIdentifier: "someViewController")
self.show(vc as! UIViewController, sender: vc)
}

附注:个人而言,我会从登录页面开始执行此操作,因为这样可以简化流程,而且我不喜欢在我的AppDelegate中添加过多的代码。如果您认为不希望已经是成员的用户看到登录屏幕,则可以从AppDelegate中执行此操作,但请注意,如果您决定采取这种方法,用户体验可能会在加载过程中受到影响。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接