【问题标题】:How to add an UINavigationController to an existing UIViewController programmatically如何以编程方式将 UINavigationController 添加到现有 UIViewController
【发布时间】:2014-04-03 02:17:23
【问题描述】:

我是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];
}

当我点击按钮时,什么也没发生。

【问题讨论】:

    标签: ios iphone objective-c uiviewcontroller uinavigationcontroller


    【解决方案1】:

    这不是UINavigationControllers 的工作方式。您需要将 A 设置为 UINavigationControllerrootViewController。 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];
    }
    

    【讨论】:

    • 就像你说的,我把你建议放在AppDelegate中的代码放在A的viewDidLoad中,和你在pressButton中的代码保持一致,但它不起作用,哪里做的请问我错了吗?
    • 那你没有按照我的建议去做。 A 必须由 UINavigationController 控制。无论你在哪里展示 A,都可以展示一个以 A 作为 rootViewController 的 UINavigation 控制器。
    • 我按照您的建议再次修改了我的代码,仍然无法正常工作。我没有添加这行代码“self.window.rootViewController = navVc;”因为在我的项目中实际上是 A 是由 UITabViewController 控制的 UIController 之一,因此根控制器应该是 tabController。关系很像这样:tabController --> (X, Y, Z, A --> B)。
    • 好的,那么在您将 A 添加到 UITabBarController 的地方,您应该添加我建议的代码。 UINavigationController 应该是您的选项卡之一的视图控制器。
    • 从 WUSTLViewController_3.h 中删除 @property UINavigationController *navigationController;。如果您阅读 UIViewController 的文档,您会看到已经有一个 navigationController 属性。我的猜测是您的合成方法正在掩盖 UIViewController 的 navigationController 属性。
    【解决方案2】:

    问题是 A 需要在导航控制器中才能推送 B。

    如果 A 已经在导航控制器中:

    [A.navigationController pushViewController:B animated:YES];
    

    如果 A 不在导航控制器中,那么您需要考虑如何布置应用的视图。

    1. 将 A 放入导航控制器中。
    2. 将 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)。

    【讨论】:

    • 我在 A 中的 viewDidLoad 中添加了以下代码: b = [[B alloc] init]; navigationController = [[UINavigationController alloc] initWithRootViewController: b]; [self presentViewController:navigationController 动画:YES 完成:nil];此外,在按钮方法中,我这样做了: [self.navigationController pushViewController:b animated:YES];但是还是不行,第一步我尝试用root A初始化导航,第二步我尝试使用它。
    猜你喜欢
    • 2011-05-23
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多