简体   繁体   English

iOS - 展开我的初始视图控制器

[英]iOS - unwind to my initial view controller

I am using an unwind segue to unwind to the initial view controller in my storyboard. 我正在使用展开segue来展开我的故事板中的初始视图控制器。 The unwind works great, I implemented this method in my initial view controller: 展开效果很好,我在初始视图控制器中实现了这个方法:

- (IBAction) unwindToInitialViewController:(UIStoryboardSegue *) unwindSegue {

}

However if I try an segue to another view controller after I do the unwind I get the following error: 但是,如果我在展开后尝试使用segue到另一个视图控制器,我会收到以下错误:

Warning: Attempt to present on whose view is not in the window hierarchy! 警告:尝试显示其视图不在窗口层次结构中!

It seems like this only occurs if I unwind to the view controller that is checked as 'Initial View Controller' in the storyboard. 看起来这只有在我放松到故事板中被检查为“初始视图控制器”的视图控制器时才会出现。 Is this a bug? 这是一个错误吗? Should I be able to unwind to that initial controller? 我应该能够放松到初始控制器吗? Other ideas? 其他想法?

EDIT: 编辑:

Here is how I perform the second segue: 以下是我执行第二个segue的方法:

[self performSegueWithIdentifier:@"mySegue" sender:nil];

I should note that this is a login/logout problem. 我应该注意这是一个登录/注销问题。 When I log in the first time the segue from my login controller to my next controller works. 当我第一次登录从登录控制器到下一个控制器的segue工作时。 When I logout I unwind to the initial view controller. 当我退出时,我放松到初始视图控制器。 I then log in again and the segue from my login controller to the next controller does not work. 然后我再次登录,从我的登录控制器到下一个控制器的segue不起作用。

EDIT 2: 编辑2:

From more research I have found its because I am using a delegate for my login. 从更多的研究中我找到了它,因为我正在使用代表登录。 Login is async, I make a call with AFNetworking and when its done I call my login delegate (the login VC in this case). 登录是异步的,我用AFNetworking打电话,当它完成后我调用我的登录委托(在这种情况下登录VC)。 At that point the login VC can segue to the view. 此时,登录VC可以转到视图。

Login code: 登录代码:

- (void) login: (NSDictionary *) parameters {
    [http.manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, NSDictionary *response) {
       [self.loginDelegate loginSuccess:response]; 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       [self.loginDelegate loginFailure:error]; 
    }];
}

My login VC which is the delegate: 我的登录名VC是委托人:

- (void) loginSuccess:(NSDictionary *) response {
    // setup user info based on response
    ...
    // Segue 
    [self performSegueWithIdentifier:@"loginSuccessSegue" sender:nil];
}

I have checked that I am in the main thread when and I segue and still no luck. 我已经检查过我是主线程的时候和我一起但仍然没有运气。 I know that AFNetworking always calls the success/failure blocks on the main thread too. 我知道AFNetworking总是调用主线程上的成功/失败块。

The tricky part. 棘手的部分。 If I change that above code to use a block and not a delegate the storyboard/segue does not get messed up and I can login and logout many times with no problem. 如果我将上面的代码更改为使用块而不是委托,则storyboard / segue不会搞砸,我可以多次登录和注销,没有任何问题。

Why does the segue work the first time with the delegate pattern, but on logout (unwind), can I not use that segue again? 为什么segue第一次使用委托模式,但在注销(展开)时,我不能再使用那个segue吗?

EDIT 3: 编辑3:

More investigation shows that on unwind my login VC viewDidAppear is called twice. 更多调查显示,在展开我的登录VC viewDidAppear被调用两次。 On initial unwind the view looks to still be on the stack, show it shows quickly and viewDidAppear is called. 在初始展开时,视图仍然在堆栈中,显示它快速显示并调用viewDidAppear。 However this is animated away quickly and viewWillAppear is called a second time with a different VC. 然而,这很快就会被动画化,而viewWillAppear会被另一个VC称为第二次。 I think this might be the root of the problem. 我认为这可能是问题的根源。 Why when I unwind to that VC is it animated away only to be animated back in? 为什么当我放松到那个VC的时候,它的动画效果只会被动画回来?

Please check, whether your loginDelegate is nil during the second login attempt. 在第二次登录尝试期间,请检查您的loginDelegate是否为nil If it is nil the "delegate calls" will just go to nowhere. 如果它是nil ,“代表电话”将无处可去。 Also please check whether the loginDelegate points to the instance you expect. 另请检查loginDelegate是否指向您期望的实例。 If it points to an "old" instance, the wrong views may be tried to be presented. 如果它指向“旧”实例,则可能尝试呈现错误的视图。

The set of methods viewDidLoad , viewDidAppear , viewWillAppear , etc. can be called in unexpected order especially when going back in navigation or presenting an ad and coming back from it. 可以以意想不到的顺序调用viewDidLoadviewDidAppearviewWillAppear等方法集,尤其是在返回导航或呈现广告并从中返回时。 If you have different initialization/setup tasks distributed among these methods, you may end up with a partly initialized view controller. 如果您在这些方法之间分配了不同的初始化/设置任务,则最终可能会使用部分初始化的视图控制器。

(Thinking about the problem I lost your statement about the error encountered, so probably the delegate is not nil.) (考虑到这个问题,我失去了关于遇到的错误的陈述,所以代表可能不是零。)

EDIT: 编辑:

I ran one of my tiny unwind test projects and there log the viewDidAppear calls: 我运行了一个小的展开测试项目并记录了viewDidAppear调用:

viewDidAppear: <ViewController: 0x7a687700>
viewDidAppear: <VC2: 0x7a70e970>
viewDidAppear: <VC3: 0x7a694d50>
unwind target
viewDidAppear: <VC2: 0x7a70e970>
viewDidAppear: <ViewController: 0x7a687700>
viewDidAppear: <VC2: 0x7a71b790>
viewDidAppear: <VC3: 0x7a694d20>
unwind target
viewDidAppear: <VC2: 0x7a71b790>
viewDidAppear: <ViewController: 0x7a687700>

Doing the unwind in VC3 briefly shows VC2 and eventually ends up at the target ViewController . VC3简要显示VC2并最终到达目标ViewController Now the second "login" leads to different instances of the view controllers. 现在第二个“登录”导致视图控制器的不同实例。

Do you keep references to "old" view controllers? 你是否保留对“旧”视图控制器的引用?

Another reason might be, that your "logout" detection fires twice (once when coming along the unwind and one more time when an intermediate or the initial view controller detects the need to login?). 另一个原因可能是,您的“注销”检测会触发两次(一次是在放松时,另一次是在中间或初始视图控制器检测到需要登录时?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM