[英]Transition to a Navigation View Controller
I have the following code: 我有以下代码:
- (IBAction)buttonPressed:(UIButton *)sender
{
//sentder.titleLabel
NSString *label = [(UIButton *)sender currentTitle];
if ([label isEqualToString:@"Register"])
{
[sender setTitle:@"Registers" forState:UIControlStateNormal];
RegisterViewController *viewCon = [[RegisterViewController alloc] init];
RegisterNavigationController *navigation = [[RegisterNavigationController alloc] init];
[navigation pushViewController:viewCon animated:YES];
//self performSegueWithIdentifier:@"MySequeIdentifier" sender:];
}
....
I have a startController with the following button code that gets called correctly. 我有一个startController,其中包含以下正确调用的按钮代码。 I created a brand new default registerViewController (UIViewController) and a registerNavigationContoller (UINavigationController class).
我创建了一个全新的默认registerViewController(UIViewController)和一个registerNavigationContoller(UINavigationController类)。 How do I get my button click to animate to the registerViewController and have that view controller have a bar with a back button?
如何获得我的按钮单击动画以创建registerViewController并使该视图控制器具有带有后退按钮的栏?
Am I doing this wrong, is the startViewController supposed to be a UINavigationController as it's just a UIViewController? 我做错了吗,startViewController是否应该是UINavigationController,因为它只是UIViewController? If so how do I get rid of the top bar on this page?
如果是这样,我如何摆脱此页面顶部的栏?
You existing View Controller (the one containing this button code) needs to be contained within a Navigation Controller. 您现有的View Controller(包含此按钮代码的View Controller)需要包含在Navigation Controller中。 The new VC will also be contained in the same Nav Controller.
新的VC也将包含在同一导航控制器中。 The Nav Controller manages the title bar and the back button navigation between these two child view controllers.
导航控制器管理这两个子视图控制器之间的标题栏和后退按钮导航。
To embed the first VC in a Nav Controller: 要将第一个VC嵌入到Nav Controller中:
if using storyboard, you can select the view controller scene and then in the menu: Editor > Embed In > Navigation Controller
如果使用情节提要,则可以选择视图控制器场景,然后在菜单中:
Editor > Embed In > Navigation Controller
if instantiating in code, first initialise the View Controller and then embed it in a new nav controller thus: 如果在代码中实例化,则首先初始化View Controller,然后将其嵌入到新的nav控制器中,从而:
UIViewController firstVC; firstVC = [[UIViewController alloc] initWithNib:@"nibname" bundle:nil]] //(example of initialising with a xib/nib file) UINavigationCOntroller navCon; navCon = [UINavigationController alloc] initWithRootViewController:firstVC];
navController navController
Now from your button code you ask your parent navigation controller to manage the transition: 现在,从您的按钮代码中,请您的父级导航控制器管理过渡:
RegisterViewController *viewCon = [[RegisterViewController alloc] init];
//take care to initialise correctly
//do you have a nib file? a storyboard scene?
[self.navigationController pushViewController:viewCon animated:YES];
Alternatively you can create a storyboard push segue between the button and a second storyboard scene - which performs the same function as this code. 或者,您可以在按钮和第二个情节提要场景之间创建一个情节提要挤塞功能-它执行与此代码相同的功能。
If you only want the navigation bar in the second VC, and not in the first, you have various options. 如果只希望在第二个VC中使用导航栏,而在第一个VC中则不需要,则可以有多种选择。
With each of your view controllers you can separately control the navigation controller's BOOL property navigationBarHidden
- eg self.navigationController.navigationBarHidden = YES
; 使用每个视图控制器,您都可以分别控制导航控制器的BOOL属性
navigationBarHidden
例如self.navigationController.navigationBarHidden = YES
;
But you may find the visual transition between navbar states unsatisfactory, and may want to consider alternative app designs. 但是您可能会发现导航栏状态之间的视觉过渡不尽人意,并且可能要考虑其他应用程序设计。 For example, you could present the second view controller from the first.
例如,您可以从第一个显示第二个视图控制器。 In this case neither vcs need a navigation controller, (your second vc could contain it's own UINavigationBar instead).
在这种情况下,两个vc都不需要导航控制器,(第二个vc可以包含它自己的UINavigationBar)。 Or the presented view controller could still be embedded in it's own navigation controller.
或者提供的视图控制器仍可以嵌入其自己的导航控制器中。 Either way you will not get an automatic back button to the first view controller and will need to implement your own (back buttons are provided only when one navigation controller is managing an array of child view controllers).
无论哪种方式,您都不会在第一个视图控制器上获得自动后退按钮,而是需要自己实现(仅当一个导航控制器正在管理一系列子视图控制器时才提供后退按钮)。
Those are just a few suggestions, you will have to play around with these - and other - ideas to work out what makes most sense for your app. 这些只是一些建议,您将必须结合这些以及其他一些想法来找出最适合您的应用的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.