如何使用左侧的后退按钮创建导航栏?

时间:2013-03-02 11:27:07

标签: ios cocoa-touch interface-builder uinavigationbar

在我的应用程序中,我尝试使用导航栏后退按钮从第二个ViewController导航到第一个ViewController,但是当我将导航栏拖放到我的应用程序的XIB中的ViewController时,我只能获得了标题。

我做错了什么?我正在使用Xcode 4.6。和.xib文件来设计视图。这是我现在得到的:

Navigation Bar Without Back Button

这就是我要找的东西:

Navigation Bar with Back Button

我是iOS应用程序开发的新手,因此解释应该是非常基本的,在哪里放置文本等等。谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

导航控制器提供了一个默认的导航栏和一个后退按钮。但后面的按钮标题更改是一个有点乱的东西

<强>编程

   UIStoryboard *astoryBoard=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
    ViewController2 *vc=[astoryBoard instantiateViewControllerWithIdentifier:@"ViewController2"];


    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Master" style: UIBarButtonItemStyleBordered target: nil action: nil];
    [[self navigationItem] setBackBarButtonItem: newBackButton];
    [self.navigationController pushViewController:vc animated:YES];

如果您使用 segue 将这些行包含在父VC

 UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Master" style: UIBarButtonItemStyleBordered target: nil action: nil];
    [[self navigationItem] setBackBarButtonItem: newBackButton];

仅适用于

代码明智

  UIStoryboard *astoryBoard=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
        ViewController2 *vc=[astoryBoard instantiateViewControllerWithIdentifier:@"ViewController2"];
        [self.navigationController pushViewController:vc animated:YES];

不是故事板

创建第二个VC的实例,然后使用此代码

YourViewController *VC=[[YourViewController alloc]initWithNibName:@"yournibname" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:VC animated:YES];

快乐编码

答案 1 :(得分:0)

在BUS2AppDelegate.h文件中添加此代码

@interface BUS2AppDelegate : UIResponder <UIApplicationDelegate>
{
     UINavigationController *navController;   //you have added this code?
}
@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;
    @end

并在BUS2AppDelegate.m文件中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        // Override point for customization after application launch.
    self.viewController = [[BUS2ViewController alloc] initWithNibName:@"BUS2ViewController" bundle:nil];

    // View Controller with each Navigational stack support.
    navController = [[UINavigationController alloc]
                     initWithRootViewController:self.viewController];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];

    return YES;
}

在第一个视图控制器中添加此代码

#import "AAlesund.h"

- (IBAction)secodVCBtnClicked:(id)sender {


    AAlesund *aAlesund = [[AAlesund alloc] initWithNibName:@"AAlesund" bundle:nil];

self.navigationItem.title = @"Master";
    [self.navigationController pushViewController:aAlesund  animated:NO];
}

要在第二个视图控制器的导航栏中添加标题,请添加此代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILabel *nav_title1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 220, 25)];
    nav_title1.font = [UIFont fontWithName:@"Arial-BoldMT" size:18];
    nav_title1.textColor = [UIColor whiteColor];
    nav_title1.adjustsFontSizeToFitWidth = NO;
    nav_title1.textAlignment = UITextAlignmentCenter;
    nav_title1.text = @"Detail";
    nav_title1.backgroundColor = [UIColor clearColor];
    self.navigationItem.titleView = nav_title1;
}

这是输出。

enter image description here