是否可以在 iphone 的静态方法中使用 self.navigationitem 属性?

发布于 2024-09-11 05:36:11 字数 1662 浏览 8 评论 0原文

在我的应用程序中,所有类中有很多视图控制器类以及表视图控制器类,我想显示导航控制器,

//creation of toolbar
UIToolbar *tool = [UIToolbar new];
tool.frame = CGRectMake(0, 0, 320, 42);
//create setting button
UIButton *bttn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
[bttn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *back=[[UIBarButtonItem alloc]initWithCustomView:bttn];

//Space
UIBarButtonItem *spbtn = [[UIBarButtonItem alloc] init];

spbtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
         UIBarButtonSystemItemFlexibleSpace   target:nil action:nil];

//Show Logo
UIButton *bttn1;
bttn1= [UIButton buttonWithType:UIButtonTypeCustom];
bttn1.frame = CGRectMake(20, 0, 230, 30);
bttn1.backgroundColor = [UIColor clearColor];
[bttn1 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
UIImage *imgss =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"pic" ofType:@"png"]];
UIImage *strechableImage1 = [imgss stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[bttn1 setBackgroundImage:strechableImage1 forState:UIControlStateNormal];
UIBarButtonItem *logo;
logo=[[UIBarButtonItem alloc]initWithCustomView:bttn1];

//assign toolbar into navigation bar
NSArray *items = [NSArray arrayWithObjects:back,logo,spbtn,nil];
[tool setItems:items animated:YES];
tool.barStyle =UIBarStyleBlackOpaque;
tool.backgroundColor = [UIColor clearColor];
self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc] initWithCustomView:tool];

我想在每个类的顶部显示导航工具栏。为此,我必须在每个类中编写此代码,但我想删除重复,我定义了一个具有静态方法的类,我尝试在该方法中执行该操作,但它生成错误,我可以做什么,简要提供一些指导。

In my application which have a lot of viewcontroller classes as well as tableviewcontroller classes in all classes i want to show navigationcontroller which have

//creation of toolbar
UIToolbar *tool = [UIToolbar new];
tool.frame = CGRectMake(0, 0, 320, 42);
//create setting button
UIButton *bttn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
[bttn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *back=[[UIBarButtonItem alloc]initWithCustomView:bttn];

//Space
UIBarButtonItem *spbtn = [[UIBarButtonItem alloc] init];

spbtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
         UIBarButtonSystemItemFlexibleSpace   target:nil action:nil];

//Show Logo
UIButton *bttn1;
bttn1= [UIButton buttonWithType:UIButtonTypeCustom];
bttn1.frame = CGRectMake(20, 0, 230, 30);
bttn1.backgroundColor = [UIColor clearColor];
[bttn1 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
UIImage *imgss =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"pic" ofType:@"png"]];
UIImage *strechableImage1 = [imgss stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[bttn1 setBackgroundImage:strechableImage1 forState:UIControlStateNormal];
UIBarButtonItem *logo;
logo=[[UIBarButtonItem alloc]initWithCustomView:bttn1];

//assign toolbar into navigation bar
NSArray *items = [NSArray arrayWithObjects:back,logo,spbtn,nil];
[tool setItems:items animated:YES];
tool.barStyle =UIBarStyleBlackOpaque;
tool.backgroundColor = [UIColor clearColor];
self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc] initWithCustomView:tool];

I want to show navigatedtoolbar at top of every class. for this i have to write this code in every class but i want to remove repetition i define a class which have static methods i try to perform that action in this method but its generate error what can i do provide some guidance in brief .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-09-18 05:36:11

是的,您可以使用

是否可以使用 self.navigationbar 。

yes you can use the

Is it possible to use self.navigationbar .

失与倦" 2024-09-18 05:36:11

我发现通过继承概念解决方案非常简单
创建一个显示导航工具栏的基类,然后我使用基类作为文件的超类

I find solution its very simple through inheritance concept
create a base class which shows navigatedtoolbar and then i use base class as a superclass of file

疧_╮線 2024-09-18 05:36:11

UINavigationController 已经有一个工具栏。如果您要启用它,您只需提供应该位于视图控制器工具栏上的按钮即可。苹果的邮件应用程序就使用了这个概念。这样做的一个好处是,在导航堆栈动画期间,您不会看到工具栏离开屏幕,这使得它看起来像是位于所有内容之上,并且工具栏上的按钮会根据当前屏幕上的 UIViewController 进行更改。

UINavigationController has a toolbar already. If you were to enable that, you can just provide the buttons that should be on the toolbar from the viewControllers. Apple's Mail application uses this concept. A benefit of this that you do not see the toolbar leaving the screen during navigation stack animations which makes it seems like it is sitting on top of everything and button's on the toolbar changes according to the UIViewController that is currently on the screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文