bipinbaglung 0 Newbie Poster

NavigationController.h

#import <UIKit/UIKit.h>
@protocol NavigationControllerDelegate;
@interface NavigationController : UINavigationController<UINavigationControllerDelegate>
@property (nonatomic , assign)id<NavigationControllerDelegate , UINavigationControllerDelegate> delegate;
-(void)cancelImagePicker:(id)sender;
@end
@protocol NavigationControllerDelegate <NSObject>

- (void)mediaPickerController:(NavigationController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;

- (void)mediaPickerControllerDidCancel:(NavigationController *)picker;

@end

NavigationController.m

#import "NavigationController.h"
#import "DataViewController.h"
@interface NavigationController ()
@end

@implementation NavigationController
@synthesize delegate;

-(id)init{
    UIViewController *controller = [[DataViewController alloc]initWithNibName:@"DataViewController" bundle:nil];
    self = [super initWithRootViewController:controller];
    if (self) {
        self.navigationBar.topItem.title = @"Data Table";
        self.navigationBar.tintColor = [UIColor clearColor];
        UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                                                        style:UIBarButtonSystemItemCancel target:self action:@selector(cancelImagePicker:)];
        self.navigationBar.topItem.rightBarButtonItem = rightButton;
    }
    return self;
}

-(void)cancelImagePicker:(id)sender{
    [delegate mediaPickerControllerDidCancel:self];   
}
@end

DataViewController.m

.....
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    secondViewController *secondController = [[secondViewController alloc]initWithNibName:@"secondViewController" bundle:nil ];
    [self.navigationController pushViewController: secondController animated:YES];
}
.......

I want to add a "cancel" button on secondViewController's navigation bar which performs the action -(void)cancelImagePicker:(id)sender; from NavigationController.m. And the same for pushed third, fourth and fifthViewController. How can this be achieved? Do i have to write code in each viewController or Can i write in common?

I have presented the NavigationController from MainViewController on buttonClick as

- (IBAction)navView:(id)sender {
    NavigationController * controller = [[NavigationController alloc]init];
    controller.delegate = self;
    [self presentViewController:controller animated:YES completion:nil];
}

and implemented the delegation methods there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.