1. UINavigationControllerDelegate协议
a. 设置代理类 nav.delegate = self;
b. 实现协议
[cpp] view plain copy
- @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
[cpp] view plain copy
- 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
[cpp] view plain copy
- UINavigationController *aNav = [[UINavigationController alloc] init];
- //然后添加一个视图进去,否则导航栏也没有意义的
- UIViewController *aViewCtrl = [[UIView alloc] initWithNibName: (*xib文件名*)];
- [aNav pushViewController:aViewCtrl animated:NO];//导航栏的第一个视图不要动画化
或者
[html] view plain copy
- BIDFirstLevelController *first = [[BIDFirstLevelController alloc] initWithStyle: UITableViewStylePlain];
- self.navController = [[TestNavgation alloc] initWithRootViewController: first];
4. 其他常用方法和属性:
本地视图.navigationItem.leftBarButtonItem //左边栏项目本地视图.
本地视图.navigationItem.rightBarButtonItem //右边栏项目本地视图.
本地视图.navigationItem.backBarButtonItem //后退栏项目本地视图.
本地视图.navigationItem.hidesBackButton //隐藏后退按钮(YES or NO)