ios 'push segues 只能在源控制器由 UINavigationController 的实例管理时使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19223761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:09:17  来源:igfitidea点击:

'Push segues can only be used when the source controller is managed by an instance of UINavigationController

ios

提问by Boggarapu

I am having a problem with this. In my root view controller I am having a textfield & one button. I am giving a condition like if i entered 0 in textfield then only it should move to next view. upto here it is working correctly. But now here is problem. Here I am having one button & given navigation to third view controller for that button. here i am getting error as

我遇到了这个问题。在我的根视图控制器中,我有一个文本字段和一个按钮。我给出的条件是,如果我在文本字段中输入 0,则只有它应该移动到下一个视图。到这里它工作正常。但现在问题来了。在这里,我有一个按钮并为该按钮导航到第三个视图控制器。在这里我收到错误

Terminating app due to uncaught exception 'NSGenericException', reason: 'Push segues can only be used when the source controller is managed by an instance of UINavigationController.'

image ref: http://img12.imageshack.us/img12/8533/myp5.png

图片参考:http: //img12.imageshack.us/img12/8533/myp5.png

and i am giving action for button in first view as below

我在第一个视图中对按钮进行操作,如下所示

- (IBAction)Submit:(id)sender {

if([tf.text isEqual:@"0"])
{

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main"   bundle:nil];

    SecondViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewControllerID" ];

    [self presentViewController:vc2 animated:YES completion:NULL];

}  
}

采纳答案by Jonathan Arbogast

After looking at your screenshot, you either need to

查看您的屏幕截图后,您要么需要

  1. Push your SecondViewControlleronto the existing navigation controller's stack instead of presenting it modally OR
  2. You need to embed your SecondViewControllerin another navigation controller and then create and present thatnavigation controller modally from your SamplesViewController
  1. 将您的内容推SecondViewController送到现有导航控制器的堆栈上,而不是以模态方式呈现或
  2. 您需要将您SecondViewController的导航控制器嵌入到另一个导航控制器中,然后从您的 SamplesViewController 模态地创建和呈现导航控制器

Either way, SecondViewControllerneeds to be embedded in a navigation controller before you can use Push Segues from it

无论哪种方式,都SecondViewController需要嵌入到导航控制器中,然后才能从中使用 Push Segues

回答by Mutawe

Go To:

去:

Editor--> Embed In --> Navigation Controller, to add Navigation Controllerto your initial view

编辑器--> 嵌入--> 导航控制器,添加Navigation Controller到您的初始视图

回答by Muhammad Irfan

Either embed your view controller in a Navigation controller or if there is a Navigation controller in the story board mak it the initial view controller

将您的视图控制器嵌入到导航控制器中,或者如果故事板中有导航控制器,则将其设为初始视图控制器

回答by chancyWu

Embed your view controller in a UINavigationControlleris a good option if there is no UINavigationControllerin the storyboard. Otherwise you can select the UINavigationControllerand select the root view controller in the inspector pane, then drag it to the UIViewControlleryou want to use as root. the screenshot is as below: enter image description here

UINavigationController如果UINavigationController情节提要中没有,则将您的视图控制器嵌入到 a 中是一个不错的选择。否则,您可以选择UINavigationController并在检查器窗格中选择根视图控制器,然后将其拖到UIViewController要用作根的 。屏幕截图如下: 在此处输入图片说明

回答by plaasm

I solved this by creating a custom UIStoryboardSegue between the UINavigationController and the destination view controller:

我通过在 UINavigationController 和目标视图控制器之间创建自定义 UIStoryboardSegue 解决了这个问题:

-(void) perform
{
    assert([self.sourceViewController isKindOfClass:[MyNavigationController class]]);
    MyNavigationController *launchController = (MyNavigationController *) self.sourceViewController;
    [launchController setViewControllers:@[self.destinationViewController] animated:YES];
}

回答by Jaywant Khedkar

Try this. It works for me.when you set root view controller to first view and use self.presentViewController,controller move to next view but instance of navigation controller is only for first view,third view required navigation instance so use self.navigationControllerinstead of presentViewController.

尝试这个。它对我有用。当您将根视图控制器设置为第一个视图并使用self.presentViewController 时,控制器移动到下一个视图,但导航控制器的实例仅适用于第一个视图,第三个视图需要导航实例,因此使用self.navigationController而不是presentViewController。

NSString * storyboardName=@"Main";
                UIStoryboard *storybord=[UIStoryboard storyboardWithName:storyboardName bundle:nil];
                 SecondViewController *vc=[storybord instantiateViewControllerWithIdentifier:@"SecondViewControllerID"];
                [self.navigationController pushViewController:vc animated:YES];

回答by RootCode

I also faced same problem. My problem was I was using modelsegue style (rather that push) for one before the current controller because of that I think it broke the Navigation chain before already.

我也面临同样的问题。我的问题是我在当前控制器之前使用了modelsegue 样式(而不是push),因为我认为它之前已经破坏了导航链。