iOS导航栏总是空的

标签 ios iphone objective-c xcode uitableview

我是 IOS 和 Objective C 开发的新手。我试图从网络加载 JSON 并将其解析为多级菜单之类的东西。单击“下一步”按钮后,用户将被定向到另一个屏幕。 我目前有基本的应用程序,其中数据加载到 RootViewController 类:UITableViewController 中。 我想在其顶部显示导航栏,其中将包含下一个屏幕按钮。 我尝试了很多选项,但在每个选项中,导航栏中都没有显示任何内容(导航栏本身就在那里,但没有标题或任何按钮)。我有基本的 XIB,其中对象下只有 TableView 。

任何指示我该怎么做。

示例代码(RootViewController.m):-

(void)viewDidLoad

{

  DBGS;

  [super viewDidLoad];

//This Failed   self.navigationController.navigationItem.rightBarButtonItem=self.navigationController.editButtonItem; 
self.navigationController.navigationItem.title=@"My Title";


//This Also Failed

UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationController.navigationItem.backBarButtonItem = _backButton;
self.navigationController.navigationItem.title=@"My Title";
[_backButton release];

//Remaining Code

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"contents" ofType:@"json"];

self.model = [[TreeListModel alloc] initWithJSONFilePath:filePath];

}

头文件(RootViewController.h)

#import <UIKit/UIKit.h>

#import "TreeListModel.h"

@interface RootViewController : UITableViewController {

    UINavigationController *navigationController;
}

@property (nonatomic, retain) TreeListModel *model;
@property (strong, nonatomic) UINavigationController *navigationController;
@property (strong, nonatomic) UIWindow *window;

@end

我无法发布应用程序图像。但在 O/P 屏幕表格 View 中(正确填充)+ 蓝色导航栏(无标题或按钮)。

谢谢

最佳答案

这段代码看起来不完整,但看起来您有点误用了导航 Controller 。

Read up on UINavigationControllers here in the Apple docs

这里有几个问题:

  1. View Controller 不需要维护对其导航 Controller 的额外引用。当 View Controller 添加到导航堆栈时,其 navigationController 属性将由导航 Controller 自动设置和控制。

  2. 您直接设置导航 Controller 的属性来设置特定 View Controller 的导航栏样式,这实际上不会执行任何操作。您需要分别设置每个 View Controller 的 navigationItem 样式,导航 Controller 将自动使用这些属性来创建实际的导航 View 。 将修改 navigationItem 的所有代码更改为不包含 navigationController

就像这样:

self.navigationItem.backBarButtonItem = _backButton;
self.navigationItem.title=@"My Title";

编辑: 还要补充一点,因为您可能会对如何使用 backBarButtonItem 感到困惑。它并不代表您将在要添加的项目上看到的后退按钮,而是表示如果您将其他内容插入堆栈时您将看到返回到当前项目的后退按钮。 文档片段:

When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. When this property is nil, the navigation item uses the value in its title property to create an appropriate back button. If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead.

关于iOS导航栏总是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22179975/

相关文章:

ios - 如何将 subview 添加到 ScrollView 的 subview

ios - 使用 self.delegate 子类化 UITextField 会导致应用程序卡住,CPU 峰值达到 100%

iphone - 我怎样才能传递 UIColor 的名字

iphone - iOS初学者:字符串串联

iphone - 如何以编程方式为黑/白 UIImageView 着色?

iphone - iOS : camera orientation

ios - 背景中的测距信标

ios - MFMessageComposeViewController 在发送消息 ios9 后不返回应用程序

ios - UITableViewCell textLabel 在选择时改变位置

iphone - 在 4 英寸 iPhone 屏幕上淡出启动图像