将按钮添加到UINavigationController子类 [英] Add buttons to UINavigationController subclass

查看:193
本文介绍了将按钮添加到UINavigationController子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建 UINavigationController 的可重用子类。所以这里是我的代码:

I'm trying to create reusable subclass of UINavigationController. So here's my code :

    @interface MainNavigationController : UINavigationController
...
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.barTintColor = primaryOrange;
    self.navigationBar.tintColor = white;

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:nil];


}

bar正在改变,但它们不出现的按钮。

我在这里错过了什么?

So the problem that the color of navigation bar is changing but the buttons they doesn't appears.
What's I've missed here ?

更新
我想为我的图片中的NavigationController使用相同的NavigationController(按钮和颜色...):

Update I want to have the same NavigationController (button and color ...) for the NavigationController in my image :

更新
所以我有subclasse我的navugationController不这样的方式:

Update So I've subclasse my navugationController unn this way :

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:nil];
    viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(showMainMenu)];

}

- (id)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        rootViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:nil];
        rootViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(showMainMenu)];


    }
    return self;
}

这个工作但是唯一的问题是,当我推视图而不是

This work's but the only problem is that when I push a view instead of having back button on left I get Always the search icon, how to fix it (I want to have search button for parent on left , and back button for child view ) ?

推荐答案

子类化设计应该以这种方式 -

Subclassing design should be in this way -

- (id)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        [self setLeftButtonToController:rootViewController];
    }
    return self;
}

- (void)setLeftButtonToController:(UIViewController *)viewController {
    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(dismissController)];
    [viewController.navigationItem setRightBarButtonItem:closeItem];
}

根据要求提供完整代码 -

As requested with full code -

//
//  MyOwnNavigationController.h
// 
//


#import <UIKit/UIKit.h>

@interface MyOwnNavigationController : UINavigationController
- (id)initWithRootViewController:(UIViewController *)rootViewController;
@end



//
//  MyOwnNavigationController.m
// 
//


#import "MyOwnNavigationController.h"

@interface MyOwnNavigationController ()

@end

@implementation MyOwnNavigationController



- (id)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        [self setLeftButtonToController:rootViewController];
    }
    return self;
}

- (void)setLeftButtonToController:(UIViewController *)viewController {
    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(dismissController)];
    [viewController.navigationItem setRightBarButtonItem:closeItem];
}


@end

使用 -

TwoViewController * viewController = [[TwoViewController alloc] initWithNibName:@"TwoViewController" bundle:nil];
    MyOwnNavigationController *navController = [[MyOwnNavigationController alloc] initWithRootViewController:viewController];

更新对于StoryBroad ----
添加以下方法到MyOwnNavigationController.m

Update For StoryBroad ---- Add below method to MyOwnNavigationController.m

- (void) loadView 
{
    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(dismissController)];
        [self.rootViewController.navigationItem setRightBarButtonItem:closeItem];
}

这篇关于将按钮添加到UINavigationController子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆