UINavigationController和UINavigationControllerDelegate

来源:互联网 发布:js委托事件 编辑:程序博客网 时间:2024/04/28 01:05

1. UINavigationControllerDelegate协议

a. 设置代理类 nav.delegate = self;

b. 实现协议

[cpp] view plain copy
  1. @protocol UINavigationControllerDelegate <NSObject>  
  2.   
  3. @optional  
  4.   
  5. // Called when the navigation controller shows a new top view controller via a push,   
  6. // pop or setting of the view controller stack.  
  7.   
  8. /*Sent to the receiver just before the navigation controller displays a view  
  9. *controller’s view and navigation item properties.*/  
  10. - (void)navigationController:(UINavigationController *)navigationController   
  11.       willShowViewController:(UIViewController *)viewController   
  12.                     animated:(BOOL)animated;  
  13.   
  14.   
  15. /*Sent to the receiver just after the navigation controller displays a view  
  16. *controller’s view and navigation item properties.*/  
  17. - (void)navigationController:(UINavigationController *)navigationController   
  18.        didShowViewController:(UIViewController *)viewController   
  19.                     animated:(BOOL)animated;  
  20.   
  21. @end  


2. UINavigationController

[cpp] view plain copy
  1. NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : UIViewController   
  2.   
  3. /* Use this initializer to make the navigation controller use your custom bar class.  
  4.    Passing nil for navigationBarClass will get you UINavigationBar, nil for toolbarClass gets UIToolbar. 
  5.    The arguments must otherwise be subclasses of the respective UIKit classes. 
  6.  */  
  7. - (instancetype)initWithNavigationBarClass:(Class)navigationBarClass toolbarClass:(Class)toolbarClass NS_AVAILABLE_IOS(5_0);  
  8.   
  9. - (id)initWithRootViewController:(UIViewController *)rootViewController; // Convenience method pushes the root view controller without animation.  
  10.   
  11. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; // Uses a horizontal slide transition. Has no effect if the view controller is already in the stack.  
  12.   
  13. - (UIViewController *)popViewControllerAnimated:(BOOL)animated; // Returns the popped controller.  
  14. - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; // Pops view controllers until the one specified is on top. Returns the popped controllers.  
  15. - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated; // Pops until there's only a single view controller left on the stack. Returns the popped controllers.  
  16.   
  17. @property(nonatomic,readonly,retain) UIViewController *topViewController; // The top view controller on the stack.  
  18. @property(nonatomic,readonly,retain) UIViewController *visibleViewController; // Return modal view controller if it exists. Otherwise the top view controller.  
  19.   
  20. @property(nonatomic,copy) NSArray *viewControllers; // The current view controller stack.  
  21. - (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.  
  22.   
  23. @property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;  
  24. - (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated; // Hide or show the navigation bar. If animated, it will transition vertically using UINavigationControllerHideShowBarDuration.  
  25. @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.  
  26.   
  27. @property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden NS_AVAILABLE_IOS(3_0); // Defaults to YES, i.e. hidden.  
  28. - (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.  
  29. @property(nonatomic,readonly) UIToolbar *toolbar NS_AVAILABLE_IOS(3_0); // For use when presenting an action sheet.  
  30.   
  31. @property(nonatomic, assign) id<UINavigationControllerDelegate> delegate;  
  32.   
  33. @end  

3. 创建UINavigationController
[cpp] view plain copy
  1. UINavigationController *aNav = [[UINavigationController alloc] init];  
  2. //然后添加一个视图进去,否则导航栏也没有意义的   
  3. UIViewController *aViewCtrl = [[UIView alloc] initWithNibName: (*xib文件名*)];  
  4. [aNav pushViewController:aViewCtrl animated:NO];//导航栏的第一个视图不要动画化  
或者

[html] view plain copy
  1. BIDFirstLevelController *first = [[BIDFirstLevelController alloc] initWithStyle: UITableViewStylePlain];  
  2. self.navController = [[TestNavgation alloc] initWithRootViewController: first];  

4. 其他常用方法和属性:

本地视图.navigationItem.leftBarButtonItem //左边栏项目本地视图.

本地视图.navigationItem.rightBarButtonItem //右边栏项目本地视图.

本地视图.navigationItem.backBarButtonItem //后退栏项目本地视图.

本地视图.navigationItem.hidesBackButton //隐藏后退按钮(YES or NO)


0 0
原创粉丝点击