iOS 教程
導(dǎo)航欄包含導(dǎo)航控制器的導(dǎo)航的按鈕。在導(dǎo)航欄中的標(biāo)題是當(dāng)前視圖控制器的標(biāo)題。
1.創(chuàng)視圖應(yīng)用程序
2.?現(xiàn)在,選擇應(yīng)用程序?Delegate.h?,添加導(dǎo)航控制器的屬性,如下所示:
#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. 更新應(yīng)用程序: didFinishLaunchingWithOptions:方法,在AppDelegate.m文件分配的導(dǎo)航控制器,并使其成為窗口的根視圖控制器,如下所示:
- (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.現(xiàn)在,通過選擇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.現(xiàn)在添加方法addNavigationBarItem并在viewDidLoad調(diào)用方法
7. 為導(dǎo)航項(xiàng)創(chuàng)建方法
8.?我們還需要?jiǎng)?chuàng)建另一種方法到另一視圖控制器?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.?現(xiàn)在當(dāng)我們運(yùn)行應(yīng)用程序時(shí)我們就會(huì)得到下面的輸出
11.?單擊?MyButton?導(dǎo)航按鈕,切換導(dǎo)航按鈕可見性
12.?單擊導(dǎo)航按鈕,顯示另一個(gè)視圖控制器,如下所示