初始化一个UINavigationbar

发布于 2024-11-18 06:29:27 字数 948 浏览 1 评论 0原文

我在制作顶部带有导航栏或控制器的表格视图时遇到问题。

我有这段代码,

- (void)viewDidLoad {
    [super viewDidLoad];

    UINavigationController *addNavCon = [[UINavigationController alloc]initWithNibName:@"Welcome" bundle:nil];
    self.navigationItem.rightBarButtonItem = self.addButtonItem;
    self.navigationItem.leftBarButtonItem = self.editButtonItem;




    [self createEditableCopyOfDatabaseIfNeeded];    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];

    NSString *documentDirectory = [self applicationDocumentsDirectory];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"notebook.plist"];

    NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
    self.Notes = tmpArray;
    [tmpArray release];



}

但是,导航栏永远不会显示,而表格则运行良好。我可以知道代码有什么问题吗?

非常感谢

I am having trouble in making a tableview with navigation bar or controller on top.

I have this piece of code,

- (void)viewDidLoad {
    [super viewDidLoad];

    UINavigationController *addNavCon = [[UINavigationController alloc]initWithNibName:@"Welcome" bundle:nil];
    self.navigationItem.rightBarButtonItem = self.addButtonItem;
    self.navigationItem.leftBarButtonItem = self.editButtonItem;




    [self createEditableCopyOfDatabaseIfNeeded];    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];

    NSString *documentDirectory = [self applicationDocumentsDirectory];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"notebook.plist"];

    NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
    self.Notes = tmpArray;
    [tmpArray release];



}

However, the navigation bar never show up, while the table is doing fine. May i know what's the problem with the code?

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎言笑 2024-11-25 06:29:27

您已经创建了 UINavigationController 的实例,但从未将其添加到任何内容中。您需要将其添加到当前层次结构中,否则它将不会出现。

如果您希望将 navController 添加到整个应用程序,那么您应该在 App Delegate 中执行此操作

- (void)applicationDidFinishLaunching:(UIApplication *)application

{

    UIViewController *rootController = [[MyRootViewController alloc] init];

    UINavigationController *navigationController = [[UINavigationController alloc]

                                initWithRootViewController:rootController];

    [rootController release];



    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [window addSubview:navigationController.view];

    [window makeKeyAndVisible];

}

You have created an instance of UINavigationController but never added it to anything. You need to add it to the current hierarchy or it will not appear.

If you wish to add the navController to the entire app, then you should do this in the App Delegate by

- (void)applicationDidFinishLaunching:(UIApplication *)application

{

    UIViewController *rootController = [[MyRootViewController alloc] init];

    UINavigationController *navigationController = [[UINavigationController alloc]

                                initWithRootViewController:rootController];

    [rootController release];



    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [window addSubview:navigationController.view];

    [window makeKeyAndVisible];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文