gpt4 book ai didi

IOS导航栏总是空的

转载 作者:行者123 更新时间:2023-11-29 12:52:11 26 4
gpt4 key购买 nike

我是 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 屏幕中有 tableview(正确填充)+ 蓝色导航栏(无标题或按钮)。

谢谢

最佳答案

这段代码看起来不完整,但看起来您有点误用了导航 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com