【问题标题】:How to create Navigation bar with back button to the left?如何使用左侧的后退按钮创建导航栏?
【发布时间】:2013-03-02 11:27:07
【问题描述】:

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

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

这就是我要找的东西:

【问题讨论】:

    标签: ios cocoa-touch interface-builder uinavigationbar


    【解决方案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];
    

    【讨论】:

    • 我只需要背,图片只是为了说明
    • 那么你可以忽略新按钮初始化并添加代码
    • 然后创建您的 vc 实例并运行代码,更新答案。确保您在 appdelegate 中使用导航控制器 rootviewcontroller
    • 我可能听起来很愚蠢,但您所说的“实例”是什么意思?我用我的第二语言编码,这是我第一次做后退按钮。非常感谢您在这里帮助我!
    • 我把它写到我的 firstviewcontroller.m 文件中,并在两个地方都更改了“YourViewController”,我需要更改“VC”吗?以及如何找到我的“笔尖”?
    【解决方案2】:

    将此代码添加到您的 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;
    }
    

    这是输出。

    【讨论】:

    • 尝试添加它,现在怎么办?导航栏和按钮会出现在第二个视图上吗?
    • 如果您有任何问题,请告诉我
    • 我什么都得不到。我需要更改文本中的哪些单词,可能会遗漏一个。
    • 你的意思是你没有在 secondVC 中看到任何东西?
    • 我试图在第二个视图的导航栏上获得一个“后退按钮” 那里已经没有“后退”按钮。我将您的方法添加到第一个 VC 中,我应该期望在第二个中看到它?感谢您帮助我!
    猜你喜欢
    • 2012-07-26
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多