繁体   English   中英

单击“条形按钮”项目后,即可显示View Controller

[英]present View Controller on click of Bar Button items

我已经包含了许多ViewControllers并且根据要求,所有ViewController必须在右侧包含两个相同的自定义BarButton项目(设置,帮助),因此我创建了一个扩展NSObject的单独类,并在其中包含了两个UIButtons

在所有ViewController中,我都导入了此类并设置barButtons像这样:

    NavigationBarButtonItems *nav=[[NavigationBarButtonItems alloc]init];
    self.navigationItem.rightBarButtonItems = [nav BarButtonItems];

其中, NavigationBarButtonItems是在-(NSArray *)BarButtonItems;包含两个自定义Button的类-(NSArray *)BarButtonItems; 方法。

一切正常。 但这是问题所在,我为其中一个barButtons创建了一个viewController,例如'settings' ,当设置barButton被单击时,由于几乎所有的viewControllers中都包含设置barButton,因此我无法弄清楚如何显示此ViewController(设置)

帮帮我。 谢谢!

在自定义类中,像这样创建按钮。

-(void)createSettingbutton:(id)FromClass {

//Create Add category Button

 UIButton *addSettingButton=[UIButton buttonWithType:UIButtonTypeCustom];
addSettingButton.frame=CGRectMake(0, 0, 30, 30);
[addSettingButton setBackgroundImage:[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"" ofType:@"png"]] forState:UIControlStateNormal];
**[addSettingButton addTarget:FromClass action:@selector(ButtonAction) forControlEvents:UIControlEventTouchUpInside];**

UIBarButtonItem *_addSettingButton=[[UIBarButtonItem alloc]initWithCustomView:addSettingButton];
[self.navigationItem setRightBarButtonItem:_addSettingButton];

[_addSettingButton release];
_addSettingButton=nil;
}

现在,在要添加设置按钮的viewcontroller中添加以下代码。

[self createSettingbutton:self];//Second self is the parameter which is passed to 'FromClass' Variable.

现在在同一个viewcontroller中添加用于设置按钮的操作

-(void)ButtonAction
    {
      [self.view addSubview:mSettingView.view];
//or
[self.navigationController pushViewController:mSettingView animated:YES]

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM