获取数据后,移至rootViewController

时间:2015-12-04 16:37:50

标签: ios segue completionhandler

我是iOS的新手 - Objective C,我正在尝试实现登录功能。我的故事板的图表如下所示:

enter image description here

因此,当有人点击“帐户”选项时,它会进入“配置文件”视图控制器,并在viewWillAppear方法中检查是否有任何用户数据。如果不是,它会对登录视图控制器执行segue。

当用户点击登录按钮并且api呼叫成功时,我想返回配置文件视图控制器。问题是,虽然我确实设法将数据恢复,但它不会让我回到Profile视图控制器。

   // code that creates the request and adds POST data and headers is not added to keep code sample to minimum


    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];

    // execute data task

    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        if (responseData != nil) {
            if ([[responseData valueForKey:@"status"] isEqualToString:@"OK"]) {
                //delegate to pass the user data to Profile view controller
                [self.delegate passUserDetails:[responseData objectForKey:@"user"]];
                [self.navigationController popToRootViewControllerAnimated:YES];

            } else if ([[responseData valueForKey:@"status"] isEqualToString:@"ERROR"]) {
                NSLog(@"%@", [responseData valueForKey:@"error"]);
            }
         } else {
            NSLog(@"Something went wrong, please try again");
         }

     }];

     [dataTask resume];

我得到的问题是在我的控制台上

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.

我在以前的帖子中读过,我可以使用dispatch_async作为问题的可能解决方案,但我不明白到底在哪里。如果这是正确的,我需要将它用作completionHandler的包装吗?有什么我做错了(可能)吗?有没有其他方法可以实现我的想法?

3 个答案:

答案 0 :(得分:1)

NSURLSession dataTaskWithRequest...调用的completionHandler在后台线程上运行。大多数UIKit调用需要从主线程进行。

将这些电话括在:

dispatch_async(dispatch_get_main_queue(), ^{
// insert your code here 
});
在你的completionHandler中

在你的情况下:

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        if (responseData != nil) {
            if ([[responseData valueForKey:@"status"] isEqualToString:@"OK"]) {
                //delegate to pass the user data to Profile view controller
                [self.delegate passUserDetails:[responseData objectForKey:@"user"]];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                };

            } else if ([[responseData valueForKey:@"status"] isEqualToString:@"ERROR"]) {
                NSLog(@"%@", [responseData valueForKey:@"error"]);
            }
         } else {
            NSLog(@"Something went wrong, please try again");
         }

     }];

如果passUserDetails拨打任何更新用户界面的电话,您也需要将其包含在内。

答案 1 :(得分:0)

我认为你应该简单地以模态方式呈现登录视图控制器,并且在登录成功之后你可以只关闭登录视图控制器.....由于模态演示用于来自用户的grabbibg短数据。所以以模态方式呈现登录控制器将是最好的实践。

答案 2 :(得分:0)

如果你想回到root视图控制器那么你可能会使用下面的代码...我正在通过我的手机回答所以键入句子Ho避免命名约定错误。 [self.navigationController popTorootviewController];

相关问题