gpt4 book ai didi

ios - UINavigationbar 所有类?

转载 作者:行者123 更新时间:2023-11-28 21:50:06 25 4
gpt4 key购买 nike

我正在为 UINavigationController 类创建类别。当我实现一个方法时,我看不到导航 Controller 上的任何控件。我已经尝试过..

.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

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

[self.navigationController setBarForCard];

告诉我建议或给我一些链接?

这是正确的方法吗?

它应该适用于所有 View Controller 。

最佳答案

简单地为 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..

关于ios - UINavigationbar 所有类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28643630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com