简体   繁体   English

使用情节提要中的按钮在NavigationController中嵌入的视图之间导航

[英]Navigate between views embedded in NavigationController using buttons in Storyboard

My app starts with a view with some buttons. 我的应用程序从带有一些按钮的视图开始。 I want to navigate to different views depending on the button pressed and pass them different parameters. 我想根据所按下的按钮导航到不同的视图,然后将不同的参数传递给它们。

My storyboard looks as follows: 我的故事板如下所示:

在此处输入图片说明 .

As you can see, pressing a button in the main view calls a table view embedded in a navigation controller. 如您所见,在主视图中按下按钮会调用嵌入在导航控制器中的表格视图。 And this is causing to me a lot of problems! 这给我带来了很多问题!

At first I don't know if I've chosen the right approach, or if I've to start my app with a navigation controller instead of a view (I've tried this solution, but in my home page I don't want a navigation bar, and also making it visible or not, isn't really nice visually). 起初,我不知道是否选择了正确的方法,或者我是否必须使用导航控制器而不是视图来启动我的应用程序(我已经尝试过此解决方案,但是在我的首页中却没有想要一个导航栏,并同时使其可见或不可见,在视觉上并不是很好。

In case you confirm the feasibility of my initial approach, how can I navigate to the desired view also passing some parameters? 如果您确认最初方法的可行性,如何在传递一些参数的情况下导航至所需视图?

------Edit: - - - 编辑:

I finally found a working solution. 我终于找到了可行的解决方案。 The Navigation controller is the first controller in my application. 导航控制器是我应用程序中的第一个控制器。 Views are connected with standard segue. 视图与标准segue相关联。

In my home view: 在我的主视图中:

- (void)viewWillAppear:(BOOL)animated
{
    [[self navigationController] setNavigationBarHidden:YES animated:YES];
}

In my table view: 在我的表格视图中:

- (void)viewWillAppear:(BOOL)animated
{
    [[self navigationController] setNavigationBarHidden:NO animated:YES];
}

In this way, when you push the button, the new view is displayed with an animation, that is the same of the navigation bar. 这样,当您按下按钮时,新视图将显示为动画,与导航栏相同。 And the same happens when you press the back button in the table view. 当您在表格视图中按返回按钮时,也会发生同样的情况。

No more lines of code are needed!!! 不需要更多的代码行!!!

The only addiction you can do to make it perfect, is to manage the first time the app is loaded to hide the navigation bar in the home view without animation. 要使其完美,唯一可以做的就是管理第一次加载该应用程序以隐藏导航栏的主视图而不显示动画。

yassa 亚萨

in a very similar case, I put the navigation controller as a initial controller in the storyboards so that the home page would be a root view controller for this navigation controller. 在非常相似的情况下,我将导航控制器作为情节提要中的初始控制器,以便主页将成为该导航控制器的根视图控制器。

So the view controllers stack would look like this: 因此,视图控制器堆栈如下所示:

Storyboards - HomeViewController - YourTableViewController 故事板-HomeViewController-YourTableViewController

If you want to hide the navigation bar in the home page you just need to hide/show it from the code using: 如果要在主页中隐藏导航栏,则只需使用以下方法从代码中隐藏/显示它:

[self.navigationController setNavigationBarHidden:YES animated:NO];

and when you want to show it in the view controller use : 当您想在视图控制器中显示它时,请使用:

[self.navigationController setNavigationBarHidden:NO animated:NO];

These lines of code should go to methods connected with displaying the view, ie viewDidAppear: or to the prepareForSegue: method 这些代码行应转到与显示视图有关的方法,即viewDidAppear:或prepareForSegue:方法

EDIT: I also added in my app some delay in pushing the next view controller: 编辑:我还在我的应用程序中添加了一些延迟,以推动下一个视图控制器:

[self.navigationController setNavigationBarHidden:NO animated:YES];

    // Showing navigation bar animation
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
        [self.navigationController pushViewController:viewController animated:YES];         
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有NavigationController的情节提要中两个视图之间的Segue / Transition - Segue/Transition between two views in a storyboard w/o a NavigationController 在TabViewController和NavigationController之间导航 - Navigate between TabViewController and NavigationController 使用UIButton在视图之间导航 - Using UIButton to navigate between views 使用情节提要板将NavigationController链接到AppDelegate - Linking NavigationController to the AppDelegate using storyboard 以编程方式导航到另一个故事板上的navigationcontroller和tabbarcontroller内的viewcontroller - Navigate programmatically to viewcontroller inside navigationcontroller & tabbarcontroller on another storyboard 使用情节提要的视图之间的过渡 - Transition between Views with Storyboard 在Swift 3中在不使用情节提要的情况下使用didSelect和navigationController在TableViewControllers。之间传递数据 - Passing data between TableViewControllers.using didSelect and navigationController without storyboard in Swift 3 在NavigationController中的视图之间进行排序无法正常运行 - Segueing between views in NavigationController is not functioning correctly 如何使用Swift在ios中使用Navigation Controller导航应用程序的所有视图(我使用的是Xib而不是Storyboard) - How to Navigate all views of application using Navigation Controller in ios using Swift (I am using Xib not Storyboard) self.navigationController始终使用故事板到故事板导航返回nil - self.navigationController always returns nil using storyboard to storyboard navigation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM