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.
It displays accurately but no event is perform. I mean popViewControllerAnimated:YES
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
}
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 method
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.
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;
}
On ViewDiDLoad
[utils showBackBarButton:self];