UINavigationController

来源:互联网 发布:pla算法 纠正 编辑:程序博客网 时间:2024/05/10 12:55

参考文章地址:http://www.devdiv.com/home.php?mod=space&uid=207446&do=blog&id=10934

http://www.cocoachina.com/bbs/read.php?tid-97671.html


UINavigationController的主要成员:

1.    UINavigationBar

navigation bar是管理controls的容器视图。Navigation controller负责管理navigation bar

 [1] 显示和隐藏Navigation Bar

[self.navigationController setNavigationBarHidden: YES];

[2] 修改Navigation Bar

Navigation Bar中,以下几个属性允许修改,包括:barStyletranslucenttintColor属性。

代码如下:

self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque; / /修改导航条的类型.有三种类型UIBarStyleBlackTranslucent,UIBarStyleDefault, UIBarStyle BlackOpaque .

self.navigationController.navigationBar.tintColor = [UIColor redcolor]; / / 修改背景颜色

 [3] navigationItems

navigationItemUIViewController的一个属性,这个属性是为UINavigationController服务的。每一个加到navigationControllerviewController都会有一个对应的navigationItem,该对象由viewController以懒加载的方式创建,在对象中堆navigationItem可以设置leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title以及prompt等属性。

隐藏默认返回按钮:[self.navigationItem setHidesBackButton:YES]

举个简单的例子:

3.1  修改中间titleView

NSArray *segmentTextContent = [NSArray arrayWithObjects:

                                                                      NSLocalizedString(@"Image", @""),

                                                                      NSLocalizedString(@"Text", @""),

                                                                      NSLocalizedString(@"Video", @""),

                                                           nil];

       UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];

       segmentedControl.selectedSegmentIndex = 0;

       segmentedControl.frame = CGRectMake(0, 0, 400, kCustomButtonHeight);

       [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

      

self.navigationItem.titleView = segmentedControl;

       [segmentedControl release];

 

3.2 修改rightBarButtonItem

UIButton *t_button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];

       NString *t_string1 = [NSString stringWithFormat:@"my"];

       [t_button setTitle:t_string1 forState:0];

       [t_button addTarget:self action:@selector(turnTwoView) forControlEvents: UIControlEventTouchUpInside];

       UIBarButtonItem *t_buttonItem = [[UIBarButtonItem alloc] initWithCustomView: t_button];

self.navigationItem.rightBarButtonItem = t_buttonItem;


设置nav 背景图片 5.0以下
@implementation UINavigationBar (CustomImage2)   
- (void)drawRect:(CGRect)rect {   
    UIImage *image = [UIImage imageNamed: @"NavBar.png"];   
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];   
    self.tintColor = [UIColor colorWithRed:kNavigationBarRed/255.0 green:kNavigationBarGreen/255.0 blue:kNavigationBarBlack/255.0 alpha:1.0f];
}
@end

大于等于5.0
-(void)loadNavigationBar{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) { 
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:kNavigationBarRed/255.0 green:kNavigationBarGreen/255.0 blue:kNavigationBarBlack/255.0 alpha:1.0f]];
        [[UISearchBar appearance]setBackgroundImage: [UIImage imageNamed: @"searchBar.png"]];
    }


事件:

当用

    LoginViewController* loginView=[[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];    [self.navigationController popToViewController:loginView animated:YES];
时运行点击按钮进入事件直接会crash掉,提示会没有这个页面。
换成

 for(UIViewController *vc in [self.navigationController viewControllers])    {        if([vc isMemberOfClass:[LoginViewController class]])        {            NSLog(@"Inside if");            [self.navigationController popToViewController:vc animated:YES];            return;        }    }
可以成功。具体的我也不是很清楚,猜是navigation用是将对象压入栈中,然而alloc的是新的对象虽然都是loginviewcontroller的对象但是内存地址不同,在识别时看作时俩个不同的,所以会报没有这个页面。