添加带有后退按钮的导航栏到应用程序

时间:2013-12-16 08:26:45

标签: ios objective-c uinavigationbar

我需要一步一步的教程来为我的项目添加一个包含后退按钮的导航栏。

我在AppDelegate中定义的rootViewController是LoginViewController。成功登录后,它将转到MainView,然后转到SingleView

如何添加导航栏和后退按钮?这是我的应用程序最不需要的东西。我已经尝试了很多东西

例如:

viewDidLoad方法中的所有内容

首先尝试

UIBarButtonItem *anotherButton          = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem  = anotherButton;

第二次尝试

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];
    self.navigationItem.rightBarButtonItem = anotherButton;

我到底写了什么RootViewController以及如何写入另一个UIViewController以获得一个按钮?

在大力水手的建议后编辑2

//Appdelegate.m

LoginViewController *viewController = [[LoginViewController alloc] init];
    UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:viewController];
    [navCon setNavigationBarHidden:NO];
    self.window.rootViewController      = viewController;

//LoginViewController.m

[self.navigationController setNavigationBarHidden:NO];

ToDoListViewController  *viewController = [[ToDoListViewController alloc] init];
            viewController.stringUserId             = //userid//;
            [self presentViewController:viewController animated:NO completion:nil];

//ToDoListViewController.m

[self.navigationController setNavigationBarHidden:NO];

UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = @"Back";

UINavigationItem *right = [[UINavigationItem alloc] initWithTitle:@"Hello!"];
right.leftBarButtonItem = myBarButtonItem;

[self.navigationController.navigationBar pushNavigationItem:right animated:YES];

仍然没有按钮!

1 个答案:

答案 0 :(得分:0)

最好的办法是将NavigationController添加到 rootViewController ,然后您还可以使用以下代码隐藏和显示NavigationBar

yourNavigationController.navigationBarHidden:YES/NO;

你可以通过

添加带有navigationController的rootViewController
LoginViewController *loginVC = [[LoginViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginVC];
navCon.navigationBarHidden = YES/NO;
self.window.rootViewController = navCon;
.
.
.