1. UINavigationControllerDelegate協議
a. 設定代理類 nav.delegate = self;
b. 實現協議
@protocol UINavigationControllerDelegate <NSObject>@optional// Called when the navigation controller shows a new top view controller via a push, // pop or setting of the view controller stack./*Sent to the receiver just before the navigation controller displays a view *controller’s view and navigation item properties.*/- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;/*Sent to the receiver just after the navigation controller displays a view *controller’s view and navigation item properties.*/- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;@end
2. UINavigationController
NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : UIViewController /* Use this initializer to make the navigation controller use your custom bar class. Passing nil for navigationBarClass will get you UINavigationBar, nil for toolbarClass gets UIToolbar. The arguments must otherwise be subclasses of the respective UIKit classes. */- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass NS_AVAILABLE_IOS(5_0);- (id)initWithRootViewController:(UIViewController *)rootViewController; // Convenience method pushes the root view controller without animation.- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; // Uses a horizontal slide transition. Has no effect if the view controller is already in the stack.- (UIViewController *)popViewControllerAnimated:(BOOL)animated; // Returns the popped controller.- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; // Pops view controllers until the one specified is on top. Returns the popped controllers.- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated; // Pops until there's only a single view controller left on the stack. Returns the popped controllers.@property(nonatomic,readonly,retain) UIViewController *topViewController; // The top view controller on the stack.@property(nonatomic,readonly,retain) UIViewController *visibleViewController; // Return modal view controller if it exists. Otherwise the top view controller.@property(nonatomic,copy) NSArray *viewControllers; // The current view controller stack.- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated NS_AVAILABLE_IOS(3_0); // If animated is YES, then simulate a push or pop depending on whether the new top view controller was previously in the stack.@property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated; // Hide or show the navigation bar. If animated, it will transition vertically using UINavigationControllerHideShowBarDuration.@property(nonatomic,readonly) UINavigationBar *navigationBar; // The navigation bar managed by the controller. Pushing, popping or setting navigation items on a managed navigation bar is not supported.@property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden NS_AVAILABLE_IOS(3_0); // Defaults to YES, i.e. hidden.- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated NS_AVAILABLE_IOS(3_0); // Hide or show the toolbar at the bottom of the screen. If animated, it will transition vertically using UINavigationControllerHideShowBarDuration.@property(nonatomic,readonly) UIToolbar *toolbar NS_AVAILABLE_IOS(3_0); // For use when presenting an action sheet.@property(nonatomic, assign) id<UINavigationControllerDelegate> delegate;@end
3. 建立UINavigationController
UINavigationController *aNav = [[UINavigationController alloc] init]; //然後添加一個視圖進去,否則導覽列也沒有意義的 UIViewController *aViewCtrl = [[UIView alloc] initWithNibName: (*xib檔案名稱*)]; [aNav pushViewController:aViewCtrl animated:NO];//導覽列的第一個視圖不要動畫化
或者
BIDFirstLevelController *first = [[BIDFirstLevelController alloc] initWithStyle: UITableViewStylePlain]; self.navController = [[TestNavgation alloc] initWithRootViewController: first];
4. 其他常用方法和屬性:
本地視圖.navigationItem.leftBarButtonItem //左側邊欄項目本地視圖.
本地視圖.navigationItem.rightBarButtonItem //右側邊欄項目本地視圖.
本地視圖.navigationItem.backBarButtonItem //後退欄項目本地視圖.
本地視圖.navigationItem.hidesBackButton //隱藏後退按鈕(YES or NO)