如何以编程方式返回具有全局功能的根视图控制器?
Posted
技术标签:
【中文标题】如何以编程方式返回具有全局功能的根视图控制器?【英文标题】:How to programmatically go back to root view controller with a global function? 【发布时间】:2015-10-02 07:08:25 【问题描述】:我有 3 个主要的 VC(MyAccount、Register、Login)。然后我有一个 Swift 文件(Manager.swift),我在其中设置了一个类函数,在注册或登录后它会关闭 VC。
Manager.swift
class Manager
class func registerUser(vc: UIViewController?, user_email : String, user_password : String)
let myUrl = NSURL(string: hostURL + "userRegister.php")
let request = NSMutableURLRequest(URL:myUrl!)
request.HTTPMethod = "POST";
// Compose a query string
let postString = "user_email=\(user_email)&user_password=\(user_password)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
data, response, error in
if error != nil
print("error=\(error)")
return
// You can print out response object
print("response = \(response)")
// Print out response body
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
//Let’s convert response sent from a server side script to a NSDictionary object:
let myJSON = try!NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary
if let parseJSON = myJSON
let status = parseJSON["status"] as? String
let msg = parseJSON["message"] as? String
print("Status: \(status)")
if(status != "Success")
UIAlert.displayAlert(vc, title: "Error", message: msg!, dismissTxt: "OK")
else
defaults.setBool(true, forKey: "isLoggedIn")
//My Failed attempt to go back to Root :C
vc?.navigationController?.popToRootViewControllerAnimated(true)
task.resume()
注册VC
buttonPressed(sender: AnyObject)
Manger.registerUser(self, user_email:username, user_password:password1234)
我使用 navigationController?.popToRootViewControllerAnimated 试图返回 它可以工作,但有时应用程序会崩溃,并吐出:
2015-10-02 14:51:01.410 Retail_Template[4551:261846] * -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished]、/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit 中的断言失败-3505.16/键盘/UIKeyboardTaskQueue.m:378 2015-10-02 14:51:01.415 Retail_Template[4551:261846] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] 只能从主线程调用。” *** 首先抛出调用堆栈: ( 0 核心基础 0x009f8a94 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x02b26e02 objc_exception_throw + 50 2 CoreFoundation 0x009f892a + [NSException raise:format:arguments:] + 138 3 基础 0x010bd3e6 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118 4 UIKit 0x021f9d2e-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 180 5 UIKit 0x019e14f2-[UIKeyboardImpl setDelegate:force:] + 703 6 UIKit 0x019e122e-[UIKeyboardImpl setDelegate:] + 60 7 UIKit 0x01ddd1d6-[UIPeripheralHost(UIKitInternal)_reloadInputViewsForResponder:] + 1208 8 UIKit 0x01de6bef-[UIPeripheralHost(UIKitInternal)_preserveInputViewsWithId:animated:reset:] + 502 9 UIKit 0x01de6c85-[UIPeripheralHost(UIKitInternal)_preserveInputViewsWithId:动画:] + 57 10 UIKit 0x0190dd0a-[UINavigationController navigationTransitionView:didStartTransition:] + 1029 11 UIKit 0x01903bf4 -[UINavigationController _startCustomTransition:] + 5104 12 UIKit 0x01913c0b-[UINavigationController _startDeferredTransitionIfNeeded:] + 801 13 UIKit 0x01914d05-[UINavigationController __viewWillLayoutSubviews] + 68 14 UIKit 0x01aded9f -[UILayoutContainerView layoutSubviews] + 252 15 UIKit 0x017cb16b-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 813 16 libobjc.A.dylib 0x02b3b059-[NSObject performSelector:withObject:] + 70 17 QuartzCore 0x003ed60c -[CALayer layoutSublayers] + 144 18 石英核心 0x003e128e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388 19 石英核心 0x003e10f2 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26 20 石英核心 0x003d3c2b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317 21 石英核心 0x00407c23 _ZN2CA11Transaction6commitEv + 589 22 石英核心 0x00407fbd _ZN2CA11Transaction14release_threadEPv + 289 23 libsystem_pthread.dylib 0x04bc32f7 _pthread_tsd_cleanup + 93 24 libsystem_pthread.dylib 0x04bc3051 _pthread_exit + 108 25 libsystem_pthread.dylib 0x04bc3734 pthread_get_stackaddr_np + 0 26 libsystem_pthread.dylib 0x04bc0e0e start_wqthread + 30 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止
不知道我在这里做错了什么。 任何解决方案将不胜感激。谢谢!
【问题讨论】:
【参考方案1】:正如你的错误所说:
只能从主线程调用。
你的操作应该被调用到主线程中。如下代码所示:
dispatch_async(dispatch_get_main_queue())
//Perform your task here.
【讨论】:
我明白了。所以这就是我所缺少的。 是的,只需将其添加到您的代码中,它就会正常工作..:)以上是关于如何以编程方式返回具有全局功能的根视图控制器?的主要内容,如果未能解决你的问题,请参考以下文章
以编程方式创建的 rootviewcontroller 未显示分配的 viewcontroller 的内容
如何在 ios 9 中以编程方式导航到另一个具有“当前上下文”表示的视图控制器,目标 C