Objective-C:从外部类执行segue - 警告:尝试显示* on *,其视图不在窗口层次结构中

时间:2021-07-31 22:25:58

as said in the title, I would like to perform a segue from another class (not extending UIViewController, but which manages the connection to the database).

如标题中所述,我想从另一个类执行segue(不扩展UIViewController,但管理与数据库的连接)。

Basically I get this error:

基本上我得到这个错误:

Warning: Attempt to present <RootViewController: 0x15680b00> on <LoginViewController: 0x15594290> whose view is not in the window hierarchy!

And the RootViewController never appears. (My RootViewController is the destination ViewController and LoginViewController is the ViewController from which I call the login method.)

并且RootViewController永远不会出现。 (我的RootViewController是目标ViewController,LoginViewController是ViewController,我从中调用login方法。)

Here's my code:

这是我的代码:

+ (void) login :(NSString*) username :(NSString*) password {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{
                             @"function": @"login",
                             @"username": username,
                             @"password": password
                             };
    [manager POST:serverURL parameters:params success:^(AFHTTPRequestOperation *operation, id response) {
        NSLog(@"Login user %@", username);
        if (200 == [[response valueForKey:@"code"] integerValue]) {
            [DatabaseManager fetchUser:[[CacheHandler instance] currentUser]];

            // Perform segue to ActivitiesViewController
            UIStoryboard *msb = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
            LoginViewController *lvc = [msb instantiateViewControllerWithIdentifier:@"LoginViewController"];
            [lvc performSegueWithIdentifier: @"segueActivities" sender:self];

            [[CacheHandler instance] setToken:[response valueForKey:@"token"]];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [AlertHelper error:@"Failed to log in: wrong password!"];
        NSLog(@"Error: %@", error);
    }];
}

I don't get it because the LoginViewController must be loaded in the window hierarchy since I call the login method with an action on a button after a while. Or maybe the LoginViewController I instantiated is not the one loaded on the screen?

我没有得到它,因为LoginViewController必须在窗口层次结构中加载,因为我在一段时间后通过对按钮的操作调用login方法。或者我实例化的LoginViewController可能不是屏幕上加载的那个?

Thanks!

谢谢!

3 个解决方案

#1


0  

It's pretty clear that your LoginViewController is not in the view hierarchy (i.e., not on the screen) given that you instantiate it and immediately segue to it without adding its view as a subview anywhere. What you probably want to do is grab a reference to the visible view controller via the app delegate.

很明显,您的LoginViewController不在视图层次结构中(即,不在屏幕上),因为您实例化它并立即转到它而不将其视图作为子视图添加到任何地方。您可能想要做的是通过应用程序委托获取对可见视图控制器的引用。

#2


0  

You create Login controller in memory but it's not a part of window. You have to wire it up somehow with storyboard flow, by modally presenting it using presentViewController Or creating segue that wires current controller and Login controller.

您在内存中创建Login控制器,但它不是窗口的一部分。你必须通过故事板流程以某种方式连接它,通过使用presentViewController进行模态呈现或创建连接当前控制器和登录控制器的segue。

#3


0  

Your code shows a class method, so it isn't associated with any instance of that class.

您的代码显示了一个类方法,因此它与该类的任何实例都没有关联。

Then, in your login method you instantiate an instance of LoginViewController, but this instance isn't presented, you have just created it. You then try and perform a segue on this new instance, resulting in the exception message, which is pretty clear, you are trying to perform a segue on a view controller that isn't in the view hierarchy

然后,在您的登录方法中,您实例化一个LoginViewController的实例,但是没有呈现此实例,您刚刚创建了它。然后,您尝试在此新实例上执行segue,从而产生异常消息,非常清楚,您尝试在不在视图层次结构中的视图控制器上执行segue

#1


0  

It's pretty clear that your LoginViewController is not in the view hierarchy (i.e., not on the screen) given that you instantiate it and immediately segue to it without adding its view as a subview anywhere. What you probably want to do is grab a reference to the visible view controller via the app delegate.

很明显,您的LoginViewController不在视图层次结构中(即,不在屏幕上),因为您实例化它并立即转到它而不将其视图作为子视图添加到任何地方。您可能想要做的是通过应用程序委托获取对可见视图控制器的引用。

#2


0  

You create Login controller in memory but it's not a part of window. You have to wire it up somehow with storyboard flow, by modally presenting it using presentViewController Or creating segue that wires current controller and Login controller.

您在内存中创建Login控制器,但它不是窗口的一部分。你必须通过故事板流程以某种方式连接它,通过使用presentViewController进行模态呈现或创建连接当前控制器和登录控制器的segue。

#3


0  

Your code shows a class method, so it isn't associated with any instance of that class.

您的代码显示了一个类方法,因此它与该类的任何实例都没有关联。

Then, in your login method you instantiate an instance of LoginViewController, but this instance isn't presented, you have just created it. You then try and perform a segue on this new instance, resulting in the exception message, which is pretty clear, you are trying to perform a segue on a view controller that isn't in the view hierarchy

然后,在您的登录方法中,您实例化一个LoginViewController的实例,但是没有呈现此实例,您刚刚创建了它。然后,您尝试在此新实例上执行segue,从而产生异常消息,非常清楚,您尝试在不在视图层次结构中的视图控制器上执行segue