在另一个类中处理 popViewControllerAnimated 之类的事件? [英] Handle popViewControllerAnimated like event in another class?

查看:31
本文介绍了在另一个类中处理 popViewControllerAnimated 之类的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在每个 ViewController 中创建一个相同的按钮.所以我决定编写一个具有此功能的类.我创建了一个类名 MyUtils 并向 UINavigationItem 添加了一个后退按钮,它显示正确,但不知道如何在该类中向其添加事件.

I have to create a same button in every ViewController. So i decided to write a class having this functionality once. I have created a class name MyUtils and added a back button to UINavigationItem and it displays correctly but don't know how to add an event to it in that class.

它显示准确,但没有执行任何事件.我的意思是 popViewControllerAnimated:YES

It displays accurately but no event is perform. I mean popViewControllerAnimated:YES

我像调用 myUtil 一样

I call myUtil like

MyUtil *utils = [[MyUtil alloc] init];

[utils NavAndBackBtn:self.navigationItem];

MyUtil.m

-(void)NavAndBackBtn:(UINavigationItem *)nav 
{

 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

[btn setImage:[UIImage imageNamed:@"icon_back.png"] forState:UIControlStateNormal];

//[btn setTarget:self action:@selector(popView) forControlEventTouchUPInside];

[button setFrame:CGRectMake(0,0,36,20);

UIBarButtonItem *barbtn = [[UIBarButton alloc] initWithCustomView:btn];

nav.leftBarButtonItem = barbtn;// this gives error

}
-(void)popView
{
// [self.navigationController popViewControllerAnimated:YES];  //not working no method in self
}

但不知道如何在 MyUtil 类中的 popViewControllerAnimated:YES 中添加事件.因为我在那里没有 self 的参考.

But don't know how to add an event to popViewControllerAnimated:YES in MyUtil class. because i don't have the reference of self there.

popView 方法

一定有办法获取类的上下文.我的意思是在这门课上的自我.并代表它添加事件.并且没有 UINavigationItem 的方法会弹出视图控制器.

there must be a way to get the context of the class . i mean self in this class. and add event on its behalf. And there is no method of UINavigationItem which will popViewController.

用简单的话我只想在我在 MyUtils 中创建的那个按钮上 popViewController 事件.但它必须在 MyUtils 类中实现.我如何在另一个类中 popViewControllerAnimated.

In Simple Words I simply want to popViewController event on that button which i created in MyUtils . But it must be implemented in MyUtils class. How can i popViewControllerAnimated in another class.

推荐答案

-(void)showBackBarButton:(UIViewController*)controller{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundColor:[UIColor redColor]];
    [btn setTitle:@"Back" forState:UIControlStateNormal];
    [btn addTarget:controller.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    [btn setFrame:CGRectMake(0,0,36,30)];
     UIBarButtonItem *barbtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
    controller.navigationItem.leftBarButtonItem=barbtn;
}

ViewDiDLoad

    [utils showBackBarButton:self];

这篇关于在另一个类中处理 popViewControllerAnimated 之类的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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