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];
}
.......
-(void)cancelImagePicker:(id)sender;
からアクションを実行する secondViewController のナビゲーション バーに「キャンセル」ボタンを追加したいと考えていますNavigationController.m
。プッシュされた 3 番目、4 番目、5 番目の ViewController についても同様です。これはどのように達成できますか?各viewControllerにコードを書く必要がありますか、それとも共通に書くことができますか?
私はボタンクリックのfromを次のようにNavigationController
提示しましたMainViewController
- (IBAction)navView:(id)sender {
NavigationController * controller = [[NavigationController alloc]init];
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
そこで委任メソッドを実装しました。