【问题标题】:Create a UINavigationController For present 2 UIViewController in Objective-C在 Objective-C 中为目前的 2 UIViewController 创建一个 UINavigationController
【发布时间】:2017-07-17 11:10:43
【问题描述】:

我有 2 个UIViewControllers 并希望在其上有 UINavigationController 和 UINavigationBar 和 UINavigationItem。但是我的代码不起作用..

这是我的代码:

#import "testView1.h"
#import "testView2.h"

@interface testView1 ()

@end

@implementation testView1

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor darkGrayColor];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];
    testView2 *detail = [testView2 new];
    [navController pushViewController:detail animated:YES];
}

【问题讨论】:

  • 你在用故事板吗?
  • 实际上你在这里所做的只是分配并初始化一个导航控制器,它带有一个已经存在于窗口中的 rootview 控制器。这一步不应该在 AppDelegate 中,以获得正确的结果
  • @Subramanian 不,我只是在情节提要中创建 UIViewController 并将其类设置为 testView1
  • @K.R.SaravanaKumar 是的,谢谢。但是怎么办呢?
  • @SaMiGiMiX 你在用 Storyboard 吗?

标签: ios objective-c uinavigationcontroller uinavigationbar uinavigationitem


【解决方案1】:

尝试在故事板中嵌入导航控制器,如下所示:

首先在情节提要中选择您的testView1。

选择导航控制器

及变化如下

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

   /// testView2 *detail = [testView2 new];
    testView2 *detail =  [self.storyboard instantiateViewControllerWithIdentifier:@"testView2 Identifier"]; // if you have add controller in storyboard
    [self.navigationController pushViewController:detail animated:YES];
}

【讨论】:

  • @SaMiGiMiX 欢迎您,很高兴为您提供帮助。
  • tnx.但我无法将 navigationBar 添加到导航控制器
  • 您不需要手动添加。导航控制器自动为你添加导航栏。
【解决方案2】:

您不会看到第一个视图控制器,因为您已经将第二个视图控制器推送到导航控制器上。

【讨论】:

  • 不,我的第一个 ViewController 颜色是 darkGray,运行后我的屏幕是 darkGray ..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 2020-03-27
  • 1970-01-01
  • 2010-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多