xcode 从模态视图控制器推送 UIViewController

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

Pushing a UIViewController from a modal view controller

iphonexcodeuinavigationcontroller

提问by tallen11

I am using a split view controller in an iPad app I am trying to make.

我正在尝试制作的 iPad 应用程序中使用拆分视图控制器。

Right now, I have a view being displayed in a modal view controller using:

现在,我使用以下命令在模态视图控制器中显示一个视图:

[self presentModalViewController:viewController animated:YES];

and that works fine, but when the user presses a button, I want the root view controller to push to another view. I am using:

这工作正常,但是当用户按下按钮时,我希望根视图控制器推送到另一个视图。我在用:

RootViewController *rvc = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
[rcv pushViewController:rvc animated:YES];

but that is not working. What should I do?

但这不起作用。我该怎么办?

--EDIT

- 编辑

Now, I am using

现在,我正在使用

PhotosViewController *pv = [[PhotosViewController alloc] initWithNibName:@"PhotosViewController" bundle:nil];
[self.parentViewController.navigationController pushViewController:pv animated:YES];
NSLog(@"Navigation Controller: %@", self.parentViewController.naviagtionController);

When I do the NSLog call, it returns nil. Why is that?

当我调用 NSLog 时,它返回 nil。这是为什么?

Once again, I am using a split view controller and am trying to push the RootViewController to a new view.

再一次,我正在使用拆分视图控制器并尝试将 RootViewController 推送到新视图。

Thanks

谢谢

回答by conmulligan

Your code isn't working because you're created a RootViewControllerinstance and then trying to push it onto itself. What you should be doing is pushing the new view controller onto the parent view controller's navigation controller:

您的代码无法正常工作,因为您创建了一个RootViewController实例,然后尝试将其推送到自身上。您应该做的是将新的视图控制器推送到父视图控制器的导航控制器上:

[self.parentViewController.navigationController pushViewController:newViewController animated:YES];

回答by William T.

I know this is an old question but I believe the proper way "now" to push a viewController onto the main navigation stack from a modal viewController is to create a "didTapShowBlahViewController" delegate on the modal (this is assuming you want the user to be finished with the existing modal view and then push a new view onto the stack). Once you have that delegate, you simply have the view that initially invoked the modal to perform dismissing the modal and pushing the next view controller when the delegate is triggered.

我知道这是一个老问题,但我相信“现在”将 viewController 从模态视图控制器推送到主导航堆栈的正确方法是在模态上创建一个“didTapShowBlahViewController”委托(这是假设您希望用户成为完成现有的模态视图,然后将新视图推送到堆栈上)。拥有该委托后,您只需拥有最初调用模态的视图,以在触发委托时执行解除模态并推送下一个视图控制器。

- (void)didTapShowBlahViewController{
    [self dismissViewControllerAnimated:YES completion: nil];
    [self performSegueWithIdentifier:@"segueToBlahViewController" sender:self];
}

This is based on Apple's View Controller Programming Guidethat specifies passing data to child view controllers and using delegates to pass data back to parents.

这是基于Apple 的 View Controller Programming Guide,该指南指定将数据传递给子视图控制器并使用委托将数据传递回父视图。

FYI: This way also ensures that the "back" button will not go back to the modal but instead to the view that invoked the modal, which is how modals are typically used.

仅供参考:这种方式还确保“后退”按钮不会返回模态,而是返回调用模态的视图,这就是模态的通常使用方式。