2

私のアプリでは、ナビゲーションバーの戻るボタンを使用して2番目のViewControllerから最初のViewControllerに移動しようとしていますが、ナビゲーションバーをアプリのXIBのViewControllerにドラッグアンドドロップすると、タイトル付きのバーを取得できます。

私は何が間違っているのですか?Xcode4.6を使用しています。ビューを設計するための.xibファイル。これが私が今得ているものです:

戻るボタンのないナビゲーションバー

これが私が探しているものです:

戻るボタン付きのナビゲーションバー

4

2 に答える 2

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];

セグエを使用している場合は、これらの行を親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];

ストーリーボードではありません

2番目のVCのインスタンスを作成してから、このコードを使用します

YourViewController *VC=[[YourViewController alloc]initWithNibName:@"yournibname" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:VC animated:YES];
于 2013-03-02T11:52:20.700 に答える
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;
}

このコードを最初のViewControllerに追加します

#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];
}

2番目のViewControllerのナビゲーションバーにタイトルを追加するには、このコードを追加します

- (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;
}

これが出力です。

ここに画像の説明を入力してください

于 2013-03-02T11:32:44.203 に答える