【问题标题】:UINavigationBar and UITableView in Xcode 4.3.2Xcode 4.3.2 中的 UINavigationBar 和 UITableView
【发布时间】:2012-05-23 19:08:15
【问题描述】:

您是否注意到在创建 UITableView 时不再设置 UINavigationBar,即使在给它一个标题或按钮之后也是如此?

现在我正在为如何在我的 UITableView 上放置导航栏而发疯。似乎真的不可能。我试图向我的 tableView 添加一个带有导航栏的子视图,但似乎毫无价值,因为当我向下滚动时,导航栏会向下滚动并且它不应该向下滚动。

关于如何实现它的任何想法?

编辑

好吧,我一如既往地选择了文件 -> 新建 -> 文件.. -> UITableView。然后我设置了一些代码,当我写的时候

self.navigationItem.title = @"MyTitle";
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;

并尝试在模拟器上测试,没有出现导航栏。

我的初始化代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"TabTitle";
        self.tabBarItem.image = [UIImage imageNamed:@"img.png"];
        self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];

    }
    return self;
}

我无法解释为什么它不再出现。我还尝试创建一个新项目并从出现导航栏的项目中导入我的类,但结果也相同。

EDIT2*

该应用程序是一个基于选项卡的应用程序。 这是从用于设置 tabBar 的 App 委托中获取的代码。

 UIViewController *viewController1, *viewController4;
    UITableViewController *viewController2, *viewController3;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        viewController1 = [[FirstViewController alloc] initWithNibName:@"First_iPhone" bundle:nil];
        viewController2 = [[Tips alloc] initWithNibName:@"Table" bundle:nil];
        viewController3 = [[Favorites alloc] initWithNibName:@"Test_iPhone" bundle:nil];
        viewController4 = [[SecondViewController alloc] initWithNibName:@"Second_iPhone" bundle:nil];
    }

【问题讨论】:

  • UI 是通过代码创建的,是的。我有另外 2 个带有 UINavigationBar 的 UIViewController(但通过 Interface Builder 设置),但没有进入我的 tableView。我查了一下,
  • 是的,它是通过代码创建的。我有 2 个其他 UIViewController 与 UINavigationBar 正常工作(但通过 IB 设置)。我检查了“navigationController is: (null)”。我的 .h 文件中有 UINavigationControllerDelegate。
  • 好吧,我想现在已经接近了 ;) 控制器似乎不在导航栏的导航堆栈上。你能发布一些代码来创建视图控制器并在屏幕上推送/动画吗?

标签: objective-c ios uitableview uinavigationcontroller uitabbarcontroller


【解决方案1】:

您正在初始化一个UITabBarController 并将4 UIViewControllers 设置为对应的UITabbarViewControllers。由于其中两个是正常的UIViewControntroller,两个是UITableViewController,所以不能有导航栏。您必须在您希望导航栏形成UINavigationController 的位置加载viewController。正确的方法是(假设 vc3 是您想要导航栏的地方):

UIViewController *viewController1, *viewController4;
UITableViewController *viewController2, viewController3;
UINavigationController *vc3NavController;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    viewController1 = [[FirstViewController alloc] initWithNibName:@"First_iPhone" bundle:nil];
    viewController2 = [[Tips alloc] initWithNibName:@"Table" bundle:nil];
    viewController3 = [[Favorites alloc] initWithNibName:@"Test_iPhone" bundle:nil];
    vc3NavController = [[UINavigationController alloc] initWithRootViewController:viewController3];        
    viewController4 = [[SecondViewController alloc] initWithNibName:@"Second_iPhone" bundle:nil];
}

然后加载 vc3NavController 而不是 viewController3 作为相应的选项卡。 所以你有: UITabBarController -> UINavigationController -> YourViewController

也许Creating a Navigation Interface 也会帮助你。

【讨论】:

  • 嗯,不应该是UINavigationController *vc3NavController; 吗?然后vc3NavController = [[UINavigationController alloc] initWithRootViewController:viewController3]; ?
  • 别担心!但是vc3NavController如何分配自己呢?我的意思是,vc3NavController = [[UINavigationController alloc]vc3NavController = [[vc3NavController alloc] 是同一个东西还是?
  • 成功了!非常感谢哥们! :)
  • 你又是对的。但是您似乎了解它的工作原理并自己弄清楚了:D
  • 呃,好像有点奇怪!谢谢你的帮助,真的很有用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多