[英]Swift Post method in AppDelegate to root ViewController
I write the function with post method to pushing to the ViewController. 我用post方法编写函数以推送到ViewController。 In post method, I'm getting the type of the user, and by type, I should push to different ViewControllers
在post方法中,我要获取用户的类型,并且按类型,我应该推送到不同的ViewControllers
class GetUser:NSObject {
class func restartapp() {
guard let window = UIApplication.shared.keyWindow else {return}
var url = baseurl + "/get-user/"
var param = ["token":APItoken.getToken()!]
window.makeKeyAndVisible()
Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
switch response.result {
case.failure(let err):
print(err)
case.success(let val):
var json = JSON(val)
print(json)
if json["user"]["role_id"].intValue == 2 {
window.rootViewController = UINavigationController.init(rootViewController: DriverMainPage())
}
if json["user"]["role_id"].intValue == 1 {
window.rootViewController = UINavigationController.init(rootViewController: MainPageController())
}
}
}
UIView.transition(with: window, duration: 0.5, options: .transitionCurlDown, animations: nil, completion: nil)
}
}
When i return this code in AppDelegate I get error NSException 当我在AppDelegate中返回此代码时,出现错误NSException
Try to run success block on main thread, like 尝试在主线程上运行成功块,例如
Reset root controller: 重置根控制器:
DispatchQueue.main.async {
if json["user"]["role_id"].intValue == 2 {
window.rootViewController = UINavigationController.init(rootViewController: DriverMainPage())
}
if json["user"]["role_id"].intValue == 1 {
window.rootViewController = UINavigationController.init(rootViewController: MainPageController())
}
}
Push on root controller: 推送根控制器:
DispatchQueue.main.async {
if json["user"]["role_id"].intValue == 2 {
(window.rootViewController as? UINavigationController)?.pushViewController(DriverMainPage(), animated: true)
}
if json["user"]["role_id"].intValue == 1 {
(window.rootViewController as? UINavigationController)?.pushViewController(MainPageController(), animated: true)
}
}
Check the method DriverMainPage()/MainPageController() returns instance of a UINavigationController or a UIViewController. 检查方法DriverMainPage()/ MainPageController()返回UINavigationController或UIViewController的实例。 These methods should return UIViewController Objects only.
这些方法应仅返回UIViewController对象。
If it returns instance of UINavigationController, you might be getting following exception. 如果它返回UINavigationController的实例,则可能会出现以下异常。 "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported' ".
“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'不支持按下导航控制器'“。
Which means you are instantiating a UINavigationController with a instance of UINavigationController which is not allowed. 这意味着您将使用不允许的UINavigationController实例实例化UINavigationController。
If it returns UIViewController instance, then please edit your question with Exception you are getting. 如果返回UIViewController实例,请使用您得到的Exception编辑您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.