IOS Navigation总结

转载自:http://blog.csdn.net/l_ch_g/article/details/8259637

Hi,请问大家:我在NavigationController里面    用这个方法来实现,隐藏NavigationBar:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {  
    if ( viewController ==  self) {  
        [navigationController setNavigationBarHidden:YES animated:animated];  
    } else if ( [navigationController isNavigationBarHidden] ) {  
        [navigationController setNavigationBarHidden:NO animated:animated];  
    } 
}  
命中注定@北京:  22:11:05



我们常常会遇到这样的问题:一个应用会有几个视图控制器没有导航栏,那么我们只需要在不需要导航栏的页面将其隐藏。

代码如下:

#import <UIKit/UIKit.h>

@class NavController;

@class FirstController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strongnonatomicUIWindow *window;

//第一次出现导航栏的页面

@property (nonatomic , retain)NavController *controller;

//导航栏视图控制器

@property (nonatomic , retain)UINavigationController *navgationController;

//系统第一次出现的页面,没有导航栏

@property (nonatomic , retain)FirstController *firstController;

@end


AppDelegate.m


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

{

    self.window = [[[UIWindow allocinitWithFrame:[[UIScreen mainScreenbounds]] autorelease];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

   // self.controller = [[NavController alloc] initWithNibName:nil bundle:nil];

    self.firstController = [[FirstController allocinitWithNibName:nil bundle:nil];

    //将第一个页面加入到导航栏根视图结点,然后在firstController中将导航栏隐藏

    self.navgationController = [[UINavigationController allocinitWithRootViewController:self.firstController];

    [self.window addSubview:self.navgationController.view];

    return YES;

}

FirstViewController.m


- (void) buttonPressed

{

    self.controller = [[NavController allocinitWithNibName:nil bundle:nil];

    [self.view removeFromSuperview];

    [self.view addSubview:self.controller.view];

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    //隐藏第一个页面的Navigation

    self.delegate = [UIApplication sharedApplication].delegate;

    self.delegate.navgationController.navigationBarHidden = YES;

    UIImage *image = [UIImage imageNamed:@"loginBackgroud.png"];

    UIImageView *imageView = [[UIImageView allocinitWithImage:image];

    [imageView setFrame:CGRectMake(00self.view.frame.size.widthself.view.frame.size.height)];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setFrame:CGRectMake(100100200140)];

    [button setTitle:@"NEXT" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

   

    [self.view addSubview:imageView];

     [self.view addSubview:button];

}

NavController.m

- (void)viewDidLoad

{

    [super viewDidLoad];

   //调用获取

    self.delegate = [UIApplication sharedApplication].delegate;

    self.delegate.navgationController.navigationBarHidden = NO;

     self.delegate.navgationController.visibleViewController.title@"NavController";

    self.controller = [[NextController allocinitWithNibName:nil bundle:nil];

    UIBarButtonItem *rightItem = [[UIBarButtonItem allocinitWithTitle:@"next"style:UIBarButtonItemStylePlain target:self action:@selector(nextPressed)];

    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"back"style:UIBarButtonItemStylePlain  target:self action:@selector(backPressed) ];

    self.delegate.navgationController.visibleViewController.navigationItem.rightBarButtonItem = rightItem;

    self.delegate.navgationController.visibleViewController.navigationItem.leftBarButtonItem = leftItem;

  //  [rightItem release];

    

}


在iPhone中对应的是UIApplication在接收到生命周期和系统事件后委托给UIApplicationDelegate来处理。每个项目都会有一个UIApplication对象来处理应用程序的生命周期和系统事件,main()函数通过UIApplicationMain()来初始化应用程序的UIApplication,如果想对应用程序的UIApplication进行操作,就只能通过[UIApplication sharedApplication]来获取到UIApplication的引用,这个方法会返回一个全局唯一的UIApplication对象给读者。

同样,读者可以通过如下代码获取应用程序的委托对象:UIApplicationDelegate* myDelegate = [[UIApplication sharedApplication] delegate];


NextController.m


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.title = @"NextController";

    UIBarButtonItem *rightItem = [[UIBarButtonItem allocinitWithTitle:@"aaa"style:UIBarButtonItemStylePlain target:self action:@selector(nextPressed)];

    self.navigationItem.rightBarButtonItem = rightItem;

    self.navigationItem.backBarButtonItem = rightItem;

    [self.navigationItem setHidesBackButton:YES];

    [rightItem release];

    

    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [leftButton setFrame:CGRectMake(1553838)];

    UIImage *image = [UIImage imageNamed:@"button_down.png"]; 

    [leftButton setImage:image forState:UIControlStateNormal];

    [leftButton addTarget:self action:@selector(backPressed)forControlEvents:UIControlEventTouchUpInside];

    //通过initWithCustomView可以自定义导航栏

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];

    self.navigationItem.leftBarButtonItem = backItem;

}


LastController.m


- (void)viewDidLoad

{

    [super viewDidLoad];

    //隐藏导航栏

     [self.navigationController setNavigationBarHidden:YES animated:YES];

    UIImage *image = [UIImage imageNamed:@"loginBackgroud.png"];

    UIImageView *imageView = [[UIImageView allocinitWithImage:image];

    [imageView setFrame:CGRectMake(00self.view.frame.size.widthself.view.frame.size.height)];

    [self.view addSubview:imageView];

}


最后,navgationBar添加图片方法如下:

      [self.navgationController.navigationBar setBackgroundImage:[UIImageimageNamed:@"button_down.png"forBarMetrics:UIBarMetricsDefault];


你可能感兴趣的:(IOS Navigation总结)