[英]Programmatically add UINavigationController in UIViewController
[英]How to add an UINavigationController to an existing UIViewController programmatically
我是iOS编程的新手,这个问题对某人来说可能看起来很愚蠢,对不起。 我有一个问题是我有一个现有的UIViewController(A)和另一个UIViewController(B),我想在A上添加一个UINavigationController,当我按下A上的一个按钮时,我将导致B.我试过了以下方法,但它没有解决问题:
在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;
}
在WUSTLViewController_3.h(A)中:
#import <UIKit/UIKit.h>
#import "WUSTLViewController_4.h"
@interface WUSTLViewController_3 : UIViewController {
WUSTLViewController_4 *viewController_4;
}
@property UINavigationController *navigationController;
@end
在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];
}
当我点击按钮时,没有任何反应。
这不是UINavigationControllers
工作方式。 您需要将A设置为UINavigationController
的rootViewController
。 UINavigationController是其他视图控制器的容器。
例如,在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;
在A
- (IBAction)pressButton:(id)sender
{
[self.navigationController pushViewController:B animated:YES];
}
问题是A需要在导航控制器中才能推送B.
如果A已经在导航控制器中:
[A.navigationController pushViewController:B animated:YES];
如果A不在导航控制器中,那么您需要考虑如何布置应用程序的视图。
如果你选择选项1,那么在加载A时,它将已经在导航控制器中。 你只需要推B.
[A.navigationController pushViewController:B animated:YES];
如果你选择选项2,那么你需要从A出示导航控制器。
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:B];
[A presentViewController:navigationController animated:YES completion:NULL];
注意:这将创建一个以B为根的新导航堆栈。 完成后,您需要有一些方法来解除导航控制器。
[A dismissViewControllerAnimated:YES completion:NULL];
如果要从B中关闭导航控制器,则需要执行以下操作。
[B.navigationController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
即来自B,获取导航控制器,并从导航控制器获取呈现视图控制器(即A)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.