ios 如何以编程方式将 UINavigationController 添加到现有 UIViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22122948/
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
How to add an UINavigationController to an existing UIViewController programmatically
提问by photosynthesis
I am newbie to iOS programming, and this problem may seem stupid to someone, I am sorry. I have a problem here is that I have an existing UIViewController (A), and another UIViewController (B), I want to add an UINavigationController to A such that when I press a button on A, I will be lead to B. I tried the following approach, but it does not solve the problem:
我是 iOS 编程的新手,这个问题对某些人来说可能很愚蠢,我很抱歉。我这里有一个问题是我有一个现有的 UIViewController (A) 和另一个 UIViewController (B),我想向 A 添加一个 UINavigationController,这样当我按下 A 上的按钮时,我会被引导到 B。我试过了以下方法,但并不能解决问题:
in AppDelegate:
在 AppDelegate 中:
@synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.tabbarController = [[UITabBarController alloc] init];
UIViewController *viewController_1 = [[WUSTLViewController_1 alloc] init];
UIViewController *viewController_2 = [[WUSTLViewController_2 alloc] init];
UIViewController *viewController_3 = [[WUSTLViewController_3 alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController: viewController_3];
self.tabbarController.viewControllers = [NSArray arrayWithObjects:viewController_1, viewController_2, navigationController, nil];
self.window.rootViewController = self.tabbarController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
In WUSTLViewController_3.h (A):
在 WUSTLViewController_3.h (A) 中:
#import <UIKit/UIKit.h>
#import "WUSTLViewController_4.h"
@interface WUSTLViewController_3 : UIViewController {
WUSTLViewController_4 *viewController_4;
}
@property UINavigationController *navigationController;
@end
In WUSTLViewController_3.m (A):
在 WUSTLViewController_3.m (A) 中:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
viewController_4 = [[WUSTLViewController_4 alloc] init];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(80, 50, 150, 40);
[myButton setTitle:@"Push" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
}
- (IBAction)pressButton:(id)sender
{
[self.navigationController pushViewController:viewController_4 animated:YES];
}
When I click the button, nothing happened.
当我点击按钮时,什么也没发生。
回答by Rob Jones
This is not how UINavigationControllers
work. You need to set A as the rootViewController
of your UINavigationController
. UINavigationController is a container for other view controllers.
这不是UINavigationControllers
工作的方式。您需要将 A 设置为rootViewController
您的UINavigationController
. UINavigationController 是其他视图控制器的容器。
For example, in your AppDelegate
you might do something like this:
例如,在你的AppDelegate
你可能会做这样的事情:
UIViewController *A = [[UIViewController alloc] init];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:A];
UIViewController *X = [[UIViewController alloc] init];
UIViewController *Y = [[UIViewController alloc] init];
UIViewController *Z = [[UIViewController alloc] init];
UITabBarController *tabVC = [[UITabBarController alloc] init];
tabVC.viewControllers = @[X, Y, Z, navVC];
self.window.rootViewController = tabVC;
And in A
而在 A
- (IBAction)pressButton:(id)sender
{
[self.navigationController pushViewController:B animated:YES];
}
回答by Jeffery Thomas
The problem is A needs to be in a navigation controller before you can push B.
问题是 A 需要在导航控制器中才能推送 B。
If A is already in a navigation controller:
如果 A 已经在导航控制器中:
[A.navigationController pushViewController:B animated:YES];
If A is not in a navigation controller, then you need to think about how you want your app's views laid out.
如果 A 不在导航控制器中,那么您需要考虑如何布置应用程序的视图。
- Put A in a navigation controller.
- Present a navigation controller from A as a presented view controller.
- 将 A 放入导航控制器中。
- 将 A 中的导航控制器呈现为呈现的视图控制器。
If you go for option 1, then by the time A is loaded, it will already be in a navigation controller. You only need to push B.
如果您选择选项 1,那么在加载 A 时,它已经在导航控制器中。你只需要推B。
[A.navigationController pushViewController:B animated:YES];
If you go for option 2, then you need to present the navigation controller from A.
如果您选择选项 2,那么您需要展示 A 中的导航控制器。
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:B];
[A presentViewController:navigationController animated:YES completion:NULL];
NOTE: This will create a new navigation stack with B as the root. You will need to have some way to dismiss the navigation controller, when you are done.
注意:这将创建一个以 B 为根的新导航堆栈。完成后,您需要有一些方法来关闭导航控制器。
[A dismissViewControllerAnimated:YES completion:NULL];
If you want to dismiss the navigation controller from within B, then you need to do the following.
如果要从 B 中关闭导航控制器,则需要执行以下操作。
[B.navigationController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
That is from B, get the navigation controller, and from the navigation controller, get the presenting view controller (which is A).
即从 B 获取导航控制器,从导航控制器获取呈现视图控制器(即 A)。