使用导航栏
导航栏包含导航按钮,导航控制器,这是一个堆栈视图控制器,可入栈和出栈。标题导航栏上的标题是当前视图的控制器
示例代码和步骤
1. 创建一个视图的应用程序
2. 现在选择APP 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
3. 现在更新应用:didFinishLaunchingWithOptions 方法在 AppDelegate.m 文件分配导航控制器,使窗口的根视图控制器如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; // Override yiibai 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; }
4.现在,添加新的类文件TempViewController通过选择File -> New ->File... -> Objective C Class,UIViewController 的子类命名类为 TempViewController。
5. 添加一个UIButton navButon 在ViewController.h 如下
// ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController { UIButton *navButton; } @end
6. 现在添加一个方法 addNavigationBarItem 并在 viewDidLoad 中调用此方法。
7. 创建导航项目动作的方法。
8. 我们还需要另一种方法来创建另一个视图控制器TempViewController的推送。
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
10. 现在,当我们运行程序时,我们会得到下面的输出
11. 点击导航按钮MyButton,推送导航按钮切换能见度
12. 点击推导航按钮按下另一个视图控制器,如下图所示。