如何以编程方式将UINavigationController添加到现有的UIViewController

pho*_*sis 5 iphone objective-c uiviewcontroller uinavigationcontroller ios

我是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;
}
Run Code Online (Sandbox Code Playgroud)

在WUSTLViewController_3.h(A)中:

#import <UIKit/UIKit.h>
#import "WUSTLViewController_4.h"

@interface WUSTLViewController_3 : UIViewController {
    WUSTLViewController_4 *viewController_4;
}

@property UINavigationController *navigationController;

@end
Run Code Online (Sandbox Code Playgroud)

在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];
}
Run Code Online (Sandbox Code Playgroud)

当我点击按钮时,没有任何反应.

Rob*_*nes 10

这不是怎么UINavigationControllers工作的.您需要将A设置为rootViewController您的UINavigationController.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;
Run Code Online (Sandbox Code Playgroud)

在A

- (IBAction)pressButton:(id)sender
{
    [self.navigationController pushViewController:B animated:YES];
}
Run Code Online (Sandbox Code Playgroud)