如何从 UIView 导航 UIViewController

Posted

技术标签:

【中文标题】如何从 UIView 导航 UIViewController【英文标题】:How to Navigate UIViewController from UIView 【发布时间】:2017-01-25 10:41:06 【问题描述】:

您好,我是 ios 开发新手,请给我一个解决方案,从 UIView 导航到 UIViewController:这是我使用过的代码,但它不起作用

acceptedJobDetailViewController *acceptedJobDetailViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"acceptedJobDetailViewController"];
[self.navigationController pushViewController:acceptedJobDetailViewController animated:YES];

我也试过下面的代码来推送视图控制器,但这也不起作用,请帮我解决这个问题:

[appDelegate.navControl pushViewController:acceptedJobDetailViewController animated:YES];

【问题讨论】:

【参考方案1】:

请在您的故事板中添加导航控制器。

step -1: select acceptedJobDetailViewController from storyboard.
step -2: now go to Editor>EmbedIn>NavigationController
step -3: now below code and run your project


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

【讨论】:

【参考方案2】:

如果您已经在您的storyboard 中添加了NavigationController,并且您尝试从UIView 类推送屏幕而不是使用以下代码:

UIViewController *superViewController =  (UIViewController*)[self nextResponder];
UINavigationController *navigationController = superViewController.navigationController;
[navigationController pushViewController:<YOUR_VIEW_CONTROLLER_OBJECT> animated:YES];

如果您还没有导航,请将其添加到您的 ViewController 之前,如下所示

Select ViewController -> Go to Editor Menu -> Embed In -> Navigation Controller

现在添加到故事板中的导航控制器现在可以执行您之前正在执行的操作。

acceptedJobDetailViewController *acceptedJobDetailViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"acceptedJobDetailViewController"];
[self.navigationController pushViewController:acceptedJobDetailViewController animated:YES];

希望这会有所帮助。

【讨论】:

【参考方案3】:

试试这个

[[(UIViewController *)[self.superview nextResponder] navigationController] pushViewController:"your viewcontroller object" animated:NO];

更新到 swift 3

(superview?.next? as? UIViewController)?.navigationController?.pushViewController("your viewcontroller object", animated: false)

【讨论】:

以上是关于如何从 UIView 导航 UIViewController的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 segue 从 UIView 导航到 UIView 控制器?

如何从 UIView 类的 xib 文件导航到 ViewController

导航离开时如何从一个 ViewController 上的 UIView 子视图中处理

一个 UIView Swift 中的多个 TableViews - 如何同时显示

从 UIView 子类推送导航控制器会导致崩溃

如何在像快门一样从上到下加载时为 UIView 设置动画?