IOS導航欄的使用
導航欄包含導航控制器的導航的按鈕。在導航欄中的標題是當前視圖控制器的標題。
示例代碼和步驟
1.創視圖應用程式
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
3. 更新應用程式: 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; }
4.現在,通過選擇File -> New ->File... -> Objective C Class 添加新的類檔TempViewController,然後將類命名 TempViewController 與 UIViewController 的子類。
5.在ViewController.h中添加navButon,如下所示
// 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. 單擊導航按鈕,顯示另一個視圖控制器,如下所示