首页 > 代码库 > UI_UINavigationController

UI_UINavigationController

创建 UINavigationController(导航控制器)

在AppDelegate.m中创建

    // 创建一个普通控制器
    RootViewController *rootVC = [[RootViewController alloc] init];

    // UINavigationController主要用来管理一组控制器(至少有一个),自带 view 可是不使用
    UINavigationController *rootNC = [[UINavigationController alloc] initWithRootViewController:rootVC];

    // 加入 控件时 是否计算导航条的宽度 默认 YES
    rootNC.navigationBar.translucent = NO;

    // 改变导航条的字体颜色
    rootNC.navigationBar.tintColor = [UIColor blueColor];

    // 改变导航条总体颜色
    rootNC.navigationBar.barTintColor = [UIColor brownColor];

    // 改变导航条标题颜色
    rootNC.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};

    // 给整个导航条加上背景图片
    [rootNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.png"] forBarMetrics:UIBarMetricsDefault];



    self.window.rootViewController = rootNC;
    [rootVC release];
    [rootNC release];

入栈和出栈

在 view 中设置 button 属性,并创建 button
入栈要引入后一个页面的控制器(controller)

@property (nonatomic, retain)UIButton *button;

在 controller 中绑定并实现点击事件

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"首页";


    // 绑定点击事件
    [self.rootView.button addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside];

}

#pragma mark - button 点击事件
- (void)buttonDidClicked:(UIButton *)sender
{
    ScendViewController *secondVC = [[ScendViewController alloc] init];
    // 压栈处理 进入下一个页面
    [self.navigationController pushViewController:secondVC animated:YES];
}

出栈

#pragma mark - 实现点击事件 出栈
- (void)buttonDidClicked:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];

}

指定页面跳转

#pragma mark - 通用方法 pop到指定控制器
- (void)button2DidClicked:(UIButton *)sender
{
    // pop 到指定控制器
//    [self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES];

    NSArray *array = self.navigationController.viewControllers;

    RootViewController *root = nil;

    for (id VC in array) {
        if ([VC isKindOfClass:[RootViewController class]]) {
            root = VC;
            break;
        }
    }

    if (root != nil) {
        [self.navigationController popToViewController:root animated:YES];
    }
}

跳转到根视图控制器

- (void)buttonDidClicked:(UIButton *)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

自己定义返回按钮

- (void)viewDidLoad {
    [super viewDidLoad];
    // 定义一个返回按钮
    // left and right 都能够
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"我的返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonItemAction:)];

    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"1.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonItemAction:)];

    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(leftButtonItemAction:)];

    // 让系统左(右)边按钮换掉
    self.navigationItem.leftBarButtonItem = leftButtonItem;

}
#pragma mark - 导航栏左边按钮的点击方法
- (void)leftButtonItemAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

UI_UINavigationController