iOS - 导航栏

  • 导航栏的使用

    导航栏包含导航控制器的导航按钮,导航控制器是一堆可以被推入和弹出的视图控制器。导航栏上的标题是当前视图控制器的标题。
  • 示例代码和步骤

    Step 1 − 创建基于视图的应用程序。
    Step 2 - 现在,选择应用程序 Delegate.h 并为导航控制器添加一个属性,如下所示 -
    
    #import <UIKit/UIKit.h>
    @class ViewController;
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    @property (strong, nonatomic) UIWindow *window;
    @property (strong, nonatomic) ViewController *viewController;
    @property (strong, nonatomic) UINavigationController *navController;
    @end
    
    Step 3 - 现在更新 application:didFinishLaunchingWithOptions: 方法在 AppDelegate.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 = [[ViewController alloc] 
       initWithNibName:@"ViewController" bundle:nil];
       
       //Navigation controller init with ViewController as root
       UINavigationController *navController = [[UINavigationController alloc]
       initWithRootViewController:self.viewController];
       self.window.rootViewController = navController;
       [self.window makeKeyAndVisible];
       return YES;
    }
    
    Step 4 − 添加一个新的类文件 TempViewController 通过选择 FileNewFile... → Objective C 类,然后将该类命名为 TempViewController,其子类为 UIViewController。
    Step 5 - 添加一个 UIButton navButonViewController.h 如下 -
    
    // ViewController.h
    #import <UIKit/UIKit.h>
    @interface ViewController : UIViewController {
       UIButton *navButton;
    }
    @end
    
    Step 6 - 添加方法 addNavigationBarItem 并调用方法 viewDidLoad.
    Step 7 − 创建导航项操作的方法。
    Step 8 − 我们还需要创建另一个方法来推送另一个视图控制器 TempViewController。
    Step 9 - 更新的 ViewController.m 如下 -
    
    // ViewController.m
    #import "ViewController.h"
    #import "TempViewController.h"
    @interface ViewController ()
    @end
    @implementation ViewController
    - (void)viewDidLoad {
       [super viewDidLoad];
       [self addNavigationBarButton];
       //Do any additional setup after loading the view, typically from a nib
    }
    - (void)didReceiveMemoryWarning {
       [super didReceiveMemoryWarning];
       // Dispose of any resources that can be recreated.
    }
    -(IBAction)pushNewView:(id)sender {
       TempViewController *tempVC =[[TempViewController alloc]
       initWithNibName:@"TempViewController" bundle:nil];
       [self.navigationController pushViewController:tempVC animated:YES];
    }
    -(IBAction)myButtonClicked:(id)sender {
       // toggle hidden state for navButton
       [navButton setHidden:!nav.hidden];
    }
    -(void)addNavigationBarButton {
       UIBarButtonItem *myNavBtn = [[UIBarButtonItem alloc] initWithTitle:
       @"MyButton" style:UIBarButtonItemStyleBordered target:
       self action:@selector(myButtonClicked:)];
       
       [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
       [self.navigationItem setRightBarButtonItem:myNavBtn];
       
       // create a navigation push button that is initially hidden
       navButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       [navButton setFrame:CGRectMake(60, 50, 200, 40)];
       [navButton setTitle:@"Push Navigation" forState:UIControlStateNormal];
       [navButton addTarget:self action:@selector(pushNewView:) 
       forControlEvents:UIControlEventTouchUpInside];
       [self.view addSubview:navButton];
       [navButton setHidden:YES];
    }
    @end
    
    Step 10 - 当我们运行应用程序时,我们将获得以下输出 -
    iOS 教程
    Step 11 − 单击导航按钮 MyButton 时,切换导航按钮的可见性。
    Step 12 - 单击推送导航按钮时,将推送另一个视图控制器,如下所示。
    iOS 教程