UINavigation栏全部class?

UINavigation bar all class?

我正在为 UINavigationController 创建类别 class.when 我实现了一个方法,我看不到导航上的任何控件 controller.i 已经尝试过..

.h 文件

进口

@interface UINavigationController (UINavigationControllFile)<UISearchBarDelegate>
-(void)setBarForCard;
@end

.m 文件

#import "UINavigationController+UINavigationControllFile.h"

@implementation UINavigationController (UINavigationControllFile)
-(void)setBarForCard
{
    [self addNatigationItemsControls];
}
-(void)addNatigationItemsControls
{
    UIBarButtonItem *slideNavigation = [[UIBarButtonItem alloc]initWithTitle:@"Slide"
                                                                       style:UIBarButtonItemStylePlain target:self action:@selector(slideAction)];
    UIBarButtonItem *homeNavigation = [[UIBarButtonItem alloc]initWithTitle:@"Home"
                                                                      style:UIBarButtonItemStylePlain target:self action:@selector(homeAction)];
    UIBarButtonItem *categoryNavigation = [[UIBarButtonItem alloc]initWithTitle:@"category"
                                                                          style:UIBarButtonItemStylePlain target:self action:@selector(categoryAction)];
    UIBarButtonItem *searchNavigation = [[UIBarButtonItem alloc]initWithTitle:@"Search"
                                                                        style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];

    UIBarButtonItem *orderHistory = [[UIBarButtonItem alloc]initWithTitle:@"History" style:UIBarButtonItemStylePlain target:self action:@selector(orderhistoryAction)];
    UIBarButtonItem *checkOut = [[UIBarButtonItem alloc] initWithTitle:@"CheckOut" style:UIBarButtonItemStylePlain target:self action:@selector(checkOutAction)];

    NSArray *arrBarBtns = [[NSArray alloc]initWithObjects:slideNavigation,homeNavigation,categoryNavigation,searchNavigation, nil];

    NSArray *arrRBtns = [[NSArray alloc]initWithObjects:orderHistory,checkOut, nil];
    self.navigationItem.rightBarButtonItems = arrRBtns;
    self.navigationItem.leftBarButtonItems = arrBarBtns;

}
-(void)homeAction
{

}

-(void)categoryAction
{

}

-(void)searchAction
{
    UISearchBar *searchBar = [[UISearchBar alloc]init];
    searchBar.delegate=self;
    searchBar.frame = CGRectMake(0, 2,600, 24);
    [searchBar setTintColor:[UIColor blueColor]];
    searchBar.showsCancelButton = YES;
    UIView *barWrapper = [[UIView alloc]initWithFrame:CGRectMake(0, 0, searchBar.frame.size.width,24)];
    [barWrapper addSubview:searchBar];
    self.navigationItem.titleView = barWrapper;

}
-(void)orderhistoryAction
{
    //    OrdersViewController *ordersViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OrdersViewController"];
    //    [self.navigationController pushViewController:ordersViewController animated:YES];
}

-(void)checkOutAction
{

}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"begin editing");

}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    NSLog(@"Text change ");

}



- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    NSLog(@"Cancel clicked");
    self.navigationItem.titleView =nil;
    self.navigationItem.leftBarButtonItems=nil;
    self.navigationItem.rightBarButtonItems=nil;
    // [self addNatigationItemsControls];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"Search Clicked %@",searchBar.text);
    NSString *temp=[NSString stringWithFormat:@"search=%@",searchBar.text];
    NSLog(@"%@ ",temp);

}
-(void)slideAction
{


}

@end

我在另一个 class 中实现,但它没有显示任何内容。

[self.navigationController setBarForCard];

告诉我建议或给我一些建议link?

这是正确的方法吗?

它应该适用于所有视图控制器。

只需为 UIViewController 创建一个类别

@interface UIViewController (UINavigationControllFile)
-(void) setBarForCard;



@implementation UIViewController (UINavigationControllFile)

-(void)setBarForCard
{
[self addNatigationItemsControls];
}
-(void)addNatigationItemsControls
 {
UIBarButtonItem *slideNavigation = [[UIBarButtonItem alloc]initWithTitle:@"Slide"
                                                                   style:UIBarButtonItemStylePlain target:self action:@selector(slideAction)];
UIBarButtonItem *homeNavigation = [[UIBarButtonItem alloc]initWithTitle:@"Home"
                                                                  style:UIBarButtonItemStylePlain target:self action:@selector(homeAction)];
UIBarButtonItem *categoryNavigation = [[UIBarButtonItem alloc]initWithTitle:@"category"
                                                                      style:UIBarButtonItemStylePlain target:self action:@selector(categoryAction)];
UIBarButtonItem *searchNavigation = [[UIBarButtonItem alloc]initWithTitle:@"Search"
                                                                    style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];

UIBarButtonItem *orderHistory = [[UIBarButtonItem alloc]initWithTitle:@"History" style:UIBarButtonItemStylePlain target:self action:@selector(orderhistoryAction)];
UIBarButtonItem *checkOut = [[UIBarButtonItem alloc] initWithTitle:@"CheckOut" style:UIBarButtonItemStylePlain target:self action:@selector(checkOutAction)];

NSArray *arrBarBtns = [[NSArray alloc]initWithObjects:slideNavigation,homeNavigation,categoryNavigation,searchNavigation, nil];

NSArray *arrRBtns = [[NSArray alloc]initWithObjects:orderHistory,checkOut, nil];
self.navigationItem.rightBarButtonItems = arrRBtns;
self.navigationItem.leftBarButtonItems = arrBarBtns;

}

然后用[self setBarForCard]调用它;从你的 ViewControllers..