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];
}
.......
我想在secondViewController的导航栏上添加一个“取消”按钮,该按钮执行来自-(void)cancelImagePicker:(id)sender;
的操作NavigationController.m
。推送的第三,第四和第五ViewController也一样。怎么能实现这一目标?我是否必须在每个viewController中编写代码或者我可以共同编写吗?
我已将NavigationController
从MainViewController
提供给buttonClick为
- (IBAction)navView:(id)sender {
NavigationController * controller = [[NavigationController alloc]init];
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
并在那里实施了委托方法。
答案 0 :(得分:1)
您可以使用通知中心代替代表,理念是,
[NSNotificationCenter DefaultCenter] addObserver]