目标C:后退按钮segue不工作



我有一个简单的2屏幕应用程序。当我按下按钮从第一个切换到第二个时,一切都成功执行了(包括动画)。但是,当我单击第二个屏幕上的后退按钮时,我得到以下警告:

Warning: Attempt to present <getTextViewController: 0x8f6aa30> on <SecondViewController: 0x946cc80> whose view is not in the window hierarchy!

编辑:请不要向我提出关于上述警告的其他问题-我已经看到了那些,他们指的是其他问题。

但是,它仍然切换回第一个屏幕。然而,segue的动画没有执行。另外:当我返回到第一个屏幕时,第一个屏幕中的信息(如输入的文本)仍然存在,而第二个屏幕中的信息每次屏幕出现时都会重置。

下面是我如何调用这两个操作:
Segue from View 1 to View 2:
Name: F21, Style: Modal, Transition: Cross Dissolve, Animation: True.
Segue from View 2 to View 1:
Name: F12, Style: Modal, Transition: Cross Dissolve, Animation: True.

getTextViewController中的代码。m(视图1):

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"F21"]){
    UIViewController *v = [segue destinationViewController];
   [self dismissViewControllerAnimated:NO completion:nil];
    v = self;
}
}
-(void)performSegue:(NSString*)str{
[self performSegueWithIdentifier:str sender:self];
}
//In some other method:
[self performSegue:@"F21"];

在SecondViewController中的代码。m(视图2):

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"F12"]){
    UIViewController *v = [segue destinationViewController];
    [self dismissViewControllerAnimated:NO completion:nil];
    v = self;
}
}
-(void)performSegue:(NSString*)str{
[self performSegueWithIdentifier:str sender:self];
}
- (IBAction)goBack:(id)sender {
[self performSegue:@"F12"];
}

我将非常感谢任何帮助来理解为什么第一个segue可以工作而第二个不能。

谢谢你,

院长

注意:这里是完整的项目- https://github.com/dean13-meet/firstIOSApp编辑:更新git.

我不太确定你在prepareForSegue中想做什么,他们没有必要在那里解雇VC。如果你想有一个简单的应用程序,你可以从VC1到VC2,然后再回来,你最好的选择是使用segueunwindSegue

在storyboard控件中从VC1上的按钮拖到VC2并选择segue类型。然后是VC1。M设置unwind segue,例如:

 - (IBAction)unwindFromViewController:(UIStoryboardSegue *)segue
 {
    //empty implementation
 }

最后,在您的VC2控件中,从后退按钮拖动到VC2上的绿色退出图标,并选择您的unwindFromViewController方法。

应该能满足你的要求。

为了简单起见,我建议使用push segue而不是modal,因为它会为您处理所有的返回按钮。如果你不喜欢导航控制器的想法,试着用下面的方法来关闭这个视图:从一个控制器返回到前一个

最新更新