ios 不支持推送导航控制器 - 执行 segues

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10817305/
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-30 18:33:48  来源:igfitidea点击:

Pushing a navigation controller is not supported- performing segues

objective-cios

提问by Lior Pollak

I created a new navigation controller in my storyboard (not programmatically!) and set it to be "Root View Controller" to a regular UIViewController and added a button in it which says- forward to the next view controller (this second view controller is a view controller which I want that will have a back button to link to the initial view controller). Now, whenever I try to link the button to the next view controller- "Pushing a navigation controller is not supported".

我在我的故事板中创建了一个新的导航控制器(不是以编程方式!)并将其设置为常规 UIViewController 的“根视图控制器”,并在其中添加了一个按钮,表示-转发到下一个视图控制器(第二个视图控制器是一个我想要的视图控制器将有一个后退按钮链接到初始视图控制器)。现在,每当我尝试将按钮链接到下一个视图控制器时 - “不支持推送导航控制器”。

Help me please, and thanks

请帮帮我,谢谢

EDIT: I accidentally subclassed UINavigationController, and not UIViewController in my class. Thank you anyway.

编辑:我不小心将 UINavigationController 子类化,而不是我的类中的 UIViewController。还是很感谢你。

采纳答案by Plasma

I've tried this and have no problems, its all done in IB with no additional code required ...

我已经试过了,没有问题,这一切都是在 IB 中完成的,不需要额外的代码......

  1. Start a new project, "Single View Application" using story boards
  2. Select storyboard and delete the views its produced.
  3. Drag on a new Navigation Controller (it will bring a table view with it)
  4. Delete the table and the table view controller, so you are just left with the Navigation Controller
  5. Drag on a normal view controller
  6. Right Click and drag from the Navigation controller to the new View and choose "Relationship - Root View Controller"
  7. Drag a "Bar Button Item" on to the Navbar which should be visible on the top of your ViewController, you can rename this Forward if you wish.
  8. Now drag on another view controller which is the one your "Forward" button will push in to view.
  9. Right Click and drag from the bar button to the 2nd View Controller, and choose "Push"
  1. 使用故事板启动一个新项目“单一视图应用程序”
  2. 选择故事板并删除其产生的视图。
  3. 拖动一个新的导航控制器(它将带来一个表格视图)
  4. 删除表和表视图控制器,所以你只剩下导航控制器
  5. 在普通视图控制器上拖动
  6. 右键单击并从导航控制器拖动到新视图,然后选择“关系 - 根视图控制器”
  7. 将“Bar Button Item”拖到应该在 ViewController 顶部可见的导航栏上,如果您愿意,可以重命名此 Forward。
  8. 现在拖动另一个视图控制器,这是您的“前进”按钮将推入查看的视图控制器。
  9. 右键单击并从栏按钮拖动到第二个视图控制器,然后选择“推”

Run the project and you will get a Single view with a Navbar and your button, clicking your button will Push the other view and give you a Back Button to return to the first View Controller. I'll try and post a picture of my storyboard if it helps.

运行该项目,您将获得一个带有导航栏和您的按钮的单一视图,单击您的按钮将推动另一个视图并为您提供一个返回按钮以返回到第一个视图控制器。如果有帮助,我会尝试发布我的故事板的图片。

PushViewControllers

推视图控制器

Plasma

等离子体

回答by LordParsley

I had the same trouble. I wanted to have a navigation controller on each storyboard, so that each could run independently, be individually debugged, and so that the look would be right with the navigation bar.

我有同样的麻烦。我想在每个故事板上都有一个导航控制器,这样每个故事板都可以独立运行,单独调试,这样导航栏的外观就会正确。

Using other approaches, I found the UINavigationController would be retained from the original storyboard -- which I didn't want -- or I'd get errors.

使用其他方法,我发现 UINavigationController 将从原始故事板中保留 - 我不想要 - 否则我会出错。

Using the AppDelegate in your view controller to set the rootViewController worked for me (borrowing segue naming conventions from Segue to another storyboard?):

在你的视图控制器中使用 AppDelegate 来设置 rootViewController 对我有用(从Segue借用 segue 命名约定到另一个故事板?):

- (void)showStartupNavigationController {
NSLog(@"-- Loading storyboard --");

//Get the storyboard from the main bundle.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Startup" bundle:nil];
//The navigation controller, not the view controller, is marked as the initial scene.
UINavigationController *theInitialViewController = [storyBoard instantiateInitialViewController];

NSLog(@"-- Loading storyboard -- Nav controller: %@", theInitialViewController);

//Remove the current navigation controller.
[self.navigationController.view removeFromSuperview];
UIWindow *window = [(AppDelegate *)[[UIApplication sharedApplication] delegate] window];
window.rootViewController = theInitialViewController;

回答by alvaro

I also wanted to do this, present a screen (that had an embedded navigation controller) when the user pushes a button.

我也想这样做,当用户按下按钮时显示一个屏幕(具有嵌入式导航控制器)。

At my first attempt, I connected the segue from the button in the fist screen to the Navigation Controller, and the app was crashing with this error "Pushing a navigation controller is not supported".

在我第一次尝试时,我将 segue 从拳头屏幕中的按钮连接到导航控制器,并且应用程序因“不支持推送导航控制器”错误而崩溃。

This is the solution I found:

这是我找到的解决方案:

Select the segue from the button in the first screen to the navigation controller. If it had an identifier, copy its name. Then delete that segue.

从第一个屏幕中的按钮中选择 segue 到导航控制器。如果它有标识符,请复制其名称。然后删除该segue。

Then create a new segue by CTRL-clicking the button in the first view controller and dragging to the VIEW CONTROLLER YOU WANT TO PRESENT(not to the Navigation Controller that is pointing at it), and select Push in the small pop up window.

然后通过按住 CTRL 键单击第一个视图控制器中的按钮并拖动到您想要展示视图控制器(而不是指向它的导航控制器)来创建一个新的 segue,然后在弹出的小窗口中选择 Push。

Then click the icon in the middle of the segue and paste the name you copied in the first step as an identifier for it.

然后单击 segue 中间的图标并粘贴您在第一步中复制的名称作为它的标识符。

IB is going to give you a warning "Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier:." Don't worry, it works perfectly.

IB 将向您发出警告“由于缺少入口点,场景无法访问,并且没有通过 -instantiateViewControllerWithIdentifier: 进行运行时访问的标识符。” 别担心,它完美地工作。

If you want to customize the string that is shown as the Back button to return, you can add this line in the viewDidLoad method OF THE VIEW CONTROLLER THAT IS BEING SHOWED AFTER THE BUTTON IS PRESSED, that is the Child view controller.
(replace "Settings" with the name you need)

如果要自定义显示为后退按钮返回的字符串,可以在按钮被按下后显示的视图控制器的 viewDidLoad 方法中添加此行,即子视图控制器。
(将“设置”替换为您需要的名称)

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.topItem.title = @"Settings";
    ...

}

回答by Plasma

To swap views Programatically you would need to select the segue and give it an Identifier like "PushView" then call it like this ....

要以编程方式交换视图,您需要选择 segue 并给它一个像“PushView”这样的标识符,然后像这样调用它......

[self performSegueWithIdentifier:@"PushView" sender:self];

That will programatically do the same as clicking the forward button. I've created you an example project with the code discussed above. Has an -(IBAction) with code in you can use for programatially changing the view.

这将以编程方式执行与单击前进按钮相同的操作。我已经使用上面讨论的代码为您创建了一个示例项目。有一个 -(IBAction) 代码,您可以使用它以编程方式更改视图。

PushView.zip

推送视图.zip